feat(grounding-wrapper): validate keyword input on initSession#98
Merged
Conversation
initSession silently emitted degenerate ids and resolved_scopes for
edge-case keywords:
- "" → id "gs--<ts>", resolved_scope ""
- 1000-char input → ID slug truncated to 16 chars, scope full-length
- "クラウド" → resolved_scope "" (Unicode stripped, nothing left)
- "クラウド-monitor" → resolved_scope "--monitor" (leading double-dash)
Add `validateKeyword(keyword)` (exported) that throws a typed Error
when the keyword is non-string, empty, longer than KEYWORD_MAX_LENGTH
(64), or its slug-normalised form has no ASCII alphanumerics. Mixed
inputs that still contain at least one [a-z0-9] after normalisation
remain accepted (e.g. "クラウド-monitor" → "monitor").
`initSession` now calls validateKeyword first so the README contract
("Public API for enforcement") matches reality. Contract section
updated to name the input invariants explicitly.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
initSessionsilently emitted degenerate ids andresolved_scopes for edge-case keywords:""gs--<ts>"""クラウド"gs---<ts>""(Unicode stripped, nothing left)"クラウド-monitor"gs---monitor-<ts>"--monitor"(leading double-dash)Reviewer of PR #91 (task 839b51f9) called out that the README's "Public API for enforcement" section claims
initSessionis deterministic inkeyword+problem, which is technically true (same input → same degenerate output) but the input domain wasn't pinned anywhere.Fix
validateKeyword(keyword: unknown): voidthat throws a typedErrorwhen the keyword is:KEYWORD_MAX_LENGTH(64) chars, ortoLowerCase→[^a-z0-9]+collapse → trim leading/trailing-).initSessioncallsvalidateKeyword(input.keyword)as its first statement.validateKeyworddirectly andinitSessioncovered.The rule preserves friendly mixed-Unicode keywords:
"クラウド-monitor"normalises to"monitor"and is still accepted. Only pure-Unicode / pure-symbol / pure-whitespace / oversize / empty inputs are rejected.Backward compatibility
Grep confirmed every existing
initSession(caller in the monorepo uses a valid ASCII slug (clawd-monitor,github-api,agent-tasks, etc.).handleScopeChangehas no production caller outsidelib.ts, so the new throw is contained.Test plan
npm testingrounding-wrapper: 42/42 pass.