@vibefield/puppeteer-adapter-claude 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/LICENSE +21 -0
  2. package/dist/detection.d.ts +78 -0
  3. package/dist/detection.d.ts.map +1 -0
  4. package/dist/detection.js +167 -0
  5. package/dist/detection.js.map +1 -0
  6. package/dist/driver.d.ts +145 -0
  7. package/dist/driver.d.ts.map +1 -0
  8. package/dist/driver.js +840 -0
  9. package/dist/driver.js.map +1 -0
  10. package/dist/generated/detection-surface.d.ts +30 -0
  11. package/dist/generated/detection-surface.d.ts.map +1 -0
  12. package/dist/generated/detection-surface.js +34 -0
  13. package/dist/generated/detection-surface.js.map +1 -0
  14. package/dist/generated/registry.d.ts +45 -0
  15. package/dist/generated/registry.d.ts.map +1 -0
  16. package/dist/generated/registry.js +287 -0
  17. package/dist/generated/registry.js.map +1 -0
  18. package/dist/index.d.ts +27 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +27 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/launch/hook-switches.d.ts +32 -0
  23. package/dist/launch/hook-switches.d.ts.map +1 -0
  24. package/dist/launch/hook-switches.js +56 -0
  25. package/dist/launch/hook-switches.js.map +1 -0
  26. package/dist/launch/invocation-grammar.d.ts +196 -0
  27. package/dist/launch/invocation-grammar.d.ts.map +1 -0
  28. package/dist/launch/invocation-grammar.js +857 -0
  29. package/dist/launch/invocation-grammar.js.map +1 -0
  30. package/dist/launch/native-invocation.d.ts +42 -0
  31. package/dist/launch/native-invocation.d.ts.map +1 -0
  32. package/dist/launch/native-invocation.js +130 -0
  33. package/dist/launch/native-invocation.js.map +1 -0
  34. package/dist/launch/prepare.d.ts +137 -0
  35. package/dist/launch/prepare.d.ts.map +1 -0
  36. package/dist/launch/prepare.js +317 -0
  37. package/dist/launch/prepare.js.map +1 -0
  38. package/dist/launch/settings.d.ts +63 -0
  39. package/dist/launch/settings.d.ts.map +1 -0
  40. package/dist/launch/settings.js +94 -0
  41. package/dist/launch/settings.js.map +1 -0
  42. package/dist/normalization/adapter.d.ts +3 -0
  43. package/dist/normalization/adapter.d.ts.map +1 -0
  44. package/dist/normalization/adapter.js +4 -0
  45. package/dist/normalization/adapter.js.map +1 -0
  46. package/dist/normalization/compaction.d.ts +4 -0
  47. package/dist/normalization/compaction.d.ts.map +1 -0
  48. package/dist/normalization/compaction.js +7 -0
  49. package/dist/normalization/compaction.js.map +1 -0
  50. package/dist/normalization/context-window.d.ts +3 -0
  51. package/dist/normalization/context-window.d.ts.map +1 -0
  52. package/dist/normalization/context-window.js +4 -0
  53. package/dist/normalization/context-window.js.map +1 -0
  54. package/dist/normalization/conversation.d.ts +3 -0
  55. package/dist/normalization/conversation.d.ts.map +1 -0
  56. package/dist/normalization/conversation.js +4 -0
  57. package/dist/normalization/conversation.js.map +1 -0
  58. package/dist/normalization/delegation.d.ts +4 -0
  59. package/dist/normalization/delegation.d.ts.map +1 -0
  60. package/dist/normalization/delegation.js +7 -0
  61. package/dist/normalization/delegation.js.map +1 -0
  62. package/dist/normalization/effort.d.ts +8 -0
  63. package/dist/normalization/effort.d.ts.map +1 -0
  64. package/dist/normalization/effort.js +16 -0
  65. package/dist/normalization/effort.js.map +1 -0
  66. package/dist/normalization/environment.d.ts +5 -0
  67. package/dist/normalization/environment.d.ts.map +1 -0
  68. package/dist/normalization/environment.js +10 -0
  69. package/dist/normalization/environment.js.map +1 -0
  70. package/dist/normalization/index.d.ts +20 -0
  71. package/dist/normalization/index.d.ts.map +1 -0
  72. package/dist/normalization/index.js +20 -0
  73. package/dist/normalization/index.js.map +1 -0
  74. package/dist/normalization/interaction.d.ts +38 -0
  75. package/dist/normalization/interaction.d.ts.map +1 -0
  76. package/dist/normalization/interaction.js +60 -0
  77. package/dist/normalization/interaction.js.map +1 -0
  78. package/dist/normalization/mode.d.ts +11 -0
  79. package/dist/normalization/mode.d.ts.map +1 -0
  80. package/dist/normalization/mode.js +37 -0
  81. package/dist/normalization/mode.js.map +1 -0
  82. package/dist/normalization/model.d.ts +5 -0
  83. package/dist/normalization/model.d.ts.map +1 -0
  84. package/dist/normalization/model.js +7 -0
  85. package/dist/normalization/model.js.map +1 -0
  86. package/dist/normalization/monitor-capabilities.d.ts +4 -0
  87. package/dist/normalization/monitor-capabilities.d.ts.map +1 -0
  88. package/dist/normalization/monitor-capabilities.js +121 -0
  89. package/dist/normalization/monitor-capabilities.js.map +1 -0
  90. package/dist/normalization/monitor.d.ts +46 -0
  91. package/dist/normalization/monitor.d.ts.map +1 -0
  92. package/dist/normalization/monitor.js +332 -0
  93. package/dist/normalization/monitor.js.map +1 -0
  94. package/dist/normalization/notification.d.ts +4 -0
  95. package/dist/normalization/notification.d.ts.map +1 -0
  96. package/dist/normalization/notification.js +9 -0
  97. package/dist/normalization/notification.js.map +1 -0
  98. package/dist/normalization/pipeline.d.ts +56 -0
  99. package/dist/normalization/pipeline.d.ts.map +1 -0
  100. package/dist/normalization/pipeline.js +472 -0
  101. package/dist/normalization/pipeline.js.map +1 -0
  102. package/dist/normalization/session.d.ts +6 -0
  103. package/dist/normalization/session.d.ts.map +1 -0
  104. package/dist/normalization/session.js +17 -0
  105. package/dist/normalization/session.js.map +1 -0
  106. package/dist/normalization/tool.d.ts +8 -0
  107. package/dist/normalization/tool.d.ts.map +1 -0
  108. package/dist/normalization/tool.js +64 -0
  109. package/dist/normalization/tool.js.map +1 -0
  110. package/dist/normalization/turn.d.ts +6 -0
  111. package/dist/normalization/turn.d.ts.map +1 -0
  112. package/dist/normalization/turn.js +15 -0
  113. package/dist/normalization/turn.js.map +1 -0
  114. package/dist/normalization/types.d.ts +47 -0
  115. package/dist/normalization/types.d.ts.map +1 -0
  116. package/dist/normalization/types.js +2 -0
  117. package/dist/normalization/types.js.map +1 -0
  118. package/dist/normalization/wire.d.ts +10 -0
  119. package/dist/normalization/wire.d.ts.map +1 -0
  120. package/dist/normalization/wire.js +75 -0
  121. package/dist/normalization/wire.js.map +1 -0
  122. package/dist/normalization/workspace.d.ts +5 -0
  123. package/dist/normalization/workspace.d.ts.map +1 -0
  124. package/dist/normalization/workspace.js +68 -0
  125. package/dist/normalization/workspace.js.map +1 -0
  126. package/dist/observation/hook-bridge.d.ts +72 -0
  127. package/dist/observation/hook-bridge.d.ts.map +1 -0
  128. package/dist/observation/hook-bridge.js +250 -0
  129. package/dist/observation/hook-bridge.js.map +1 -0
  130. package/dist/observation/otel/actor.d.ts +14 -0
  131. package/dist/observation/otel/actor.d.ts.map +1 -0
  132. package/dist/observation/otel/actor.js +44 -0
  133. package/dist/observation/otel/actor.js.map +1 -0
  134. package/dist/observation/otel/capability.d.ts +4 -0
  135. package/dist/observation/otel/capability.d.ts.map +1 -0
  136. package/dist/observation/otel/capability.js +24 -0
  137. package/dist/observation/otel/capability.js.map +1 -0
  138. package/dist/observation/otel/index.d.ts +6 -0
  139. package/dist/observation/otel/index.d.ts.map +1 -0
  140. package/dist/observation/otel/index.js +6 -0
  141. package/dist/observation/otel/index.js.map +1 -0
  142. package/dist/observation/otel/model.d.ts +9 -0
  143. package/dist/observation/otel/model.d.ts.map +1 -0
  144. package/dist/observation/otel/model.js +16 -0
  145. package/dist/observation/otel/model.js.map +1 -0
  146. package/dist/observation/otel/usage.d.ts +16 -0
  147. package/dist/observation/otel/usage.d.ts.map +1 -0
  148. package/dist/observation/otel/usage.js +75 -0
  149. package/dist/observation/otel/usage.js.map +1 -0
  150. package/dist/observation/otel/wire.d.ts +15 -0
  151. package/dist/observation/otel/wire.d.ts.map +1 -0
  152. package/dist/observation/otel/wire.js +147 -0
  153. package/dist/observation/otel/wire.js.map +1 -0
  154. package/dist/observation/semantic-provider.d.ts +45 -0
  155. package/dist/observation/semantic-provider.d.ts.map +1 -0
  156. package/dist/observation/semantic-provider.js +8 -0
  157. package/dist/observation/semantic-provider.js.map +1 -0
  158. package/dist/observation/status-channel.d.ts +70 -0
  159. package/dist/observation/status-channel.d.ts.map +1 -0
  160. package/dist/observation/status-channel.js +268 -0
  161. package/dist/observation/status-channel.js.map +1 -0
  162. package/dist/observation/transcript/conversation.d.ts +6 -0
  163. package/dist/observation/transcript/conversation.d.ts.map +1 -0
  164. package/dist/observation/transcript/conversation.js +170 -0
  165. package/dist/observation/transcript/conversation.js.map +1 -0
  166. package/dist/observation/transcript/index.d.ts +5 -0
  167. package/dist/observation/transcript/index.d.ts.map +1 -0
  168. package/dist/observation/transcript/index.js +5 -0
  169. package/dist/observation/transcript/index.js.map +1 -0
  170. package/dist/observation/transcript/provider.d.ts +7 -0
  171. package/dist/observation/transcript/provider.d.ts.map +1 -0
  172. package/dist/observation/transcript/provider.js +132 -0
  173. package/dist/observation/transcript/provider.js.map +1 -0
  174. package/dist/observation/transcript/services/jsonl-reader.d.ts +12 -0
  175. package/dist/observation/transcript/services/jsonl-reader.d.ts.map +1 -0
  176. package/dist/observation/transcript/services/jsonl-reader.js +140 -0
  177. package/dist/observation/transcript/services/jsonl-reader.js.map +1 -0
  178. package/dist/observation/transcript/services/yield.d.ts +3 -0
  179. package/dist/observation/transcript/services/yield.d.ts.map +1 -0
  180. package/dist/observation/transcript/services/yield.js +15 -0
  181. package/dist/observation/transcript/services/yield.js.map +1 -0
  182. package/dist/observation/transcript/wire.d.ts +26 -0
  183. package/dist/observation/transcript/wire.d.ts.map +1 -0
  184. package/dist/observation/transcript/wire.js +89 -0
  185. package/dist/observation/transcript/wire.js.map +1 -0
  186. package/dist/services/account-usage.d.ts +86 -0
  187. package/dist/services/account-usage.d.ts.map +1 -0
  188. package/dist/services/account-usage.js +813 -0
  189. package/dist/services/account-usage.js.map +1 -0
  190. package/dist/services/distribution.d.ts +64 -0
  191. package/dist/services/distribution.d.ts.map +1 -0
  192. package/dist/services/distribution.js +255 -0
  193. package/dist/services/distribution.js.map +1 -0
  194. package/dist/services/event-source.d.ts +29 -0
  195. package/dist/services/event-source.d.ts.map +1 -0
  196. package/dist/services/event-source.js +38 -0
  197. package/dist/services/event-source.js.map +1 -0
  198. package/dist/services/extensions.d.ts +46 -0
  199. package/dist/services/extensions.d.ts.map +1 -0
  200. package/dist/services/extensions.js +1263 -0
  201. package/dist/services/extensions.js.map +1 -0
  202. package/dist/services/models.d.ts +6 -0
  203. package/dist/services/models.d.ts.map +1 -0
  204. package/dist/services/models.js +90 -0
  205. package/dist/services/models.js.map +1 -0
  206. package/dist/services/profiles.d.ts +48 -0
  207. package/dist/services/profiles.d.ts.map +1 -0
  208. package/dist/services/profiles.js +176 -0
  209. package/dist/services/profiles.js.map +1 -0
  210. package/dist/services/prompt.d.ts +39 -0
  211. package/dist/services/prompt.d.ts.map +1 -0
  212. package/dist/services/prompt.js +107 -0
  213. package/dist/services/prompt.js.map +1 -0
  214. package/package.json +46 -3
  215. package/README.md +0 -3
@@ -0,0 +1,857 @@
1
+ /**
2
+ * Claude Code's command line as the installed CLI reads it: one declarative table and the one
3
+ * reader that walks argv with it. The reader states what a typed `claude …` IS — a passthrough
4
+ * (help, a management command, a one-shot, a session that lives elsewhere), a session the managed
5
+ * launch cannot carry (a clause a person reads), or a session it runs with the person's own
6
+ * arguments carried. What a host does with that is the host's policy.
7
+ *
8
+ * The CLI reads argv in two stages and so does this reader, in the CLI's own order. First its
9
+ * entry looks at the raw argv before any parser runs: literal tokens, some only as the first
10
+ * argument, some anywhere, some only before `--` ({@link CLAUDE_ENTRY_RULES},
11
+ * {@link CLAUDE_SUBCOMMANDS}' `dispatch`). Then commander parses the rest (12.x semantics in this
12
+ * build: options anywhere until `--`; unknown options refused; excess operands dropped; positional
13
+ * options, so a subcommand selected as the first operand owns everything after it; subcommands
14
+ * registered only when the argv does not ask for `-p`/`--print`).
15
+ *
16
+ * Every row is evidence-bound to one CLI version: the census test holds the table to the
17
+ * captured `claude --help` in both directions. Evidence per row, and the probes behind the
18
+ * classes: packages/surface/vendors/claude/findings.md §7 and probes/claude@2.1.283/.
19
+ */
20
+ /** The Claude Code version whose grammar this table is. */
21
+ export const CLAUDE_INVOCATION_GRAMMAR_VERSION = '2.1.283';
22
+ const SESSION_ID_OWNED = 'the managed launch gives the session its id itself';
23
+ const HOOKS_OFF = 'it turns off the hooks a managed session is observed through';
24
+ const PRINT_ONLY = 'it only works with -p/--print; an interactive session does not take it';
25
+ /** Every option the root command accepts at {@link CLAUDE_INVOCATION_GRAMMAR_VERSION}, in the help's order. */
26
+ export const CLAUDE_OPTIONS = [
27
+ { names: ['--add-dir'], arity: 'variadic', class: 'carried', why: 'more directories the tools may reach' },
28
+ { names: ['--agent'], arity: 'one', class: 'carried', why: 'the agent the session runs as' },
29
+ { names: ['--agents'], arity: 'one', class: 'carried', why: 'custom agents for the session' },
30
+ {
31
+ names: ['--allow-dangerously-skip-permissions'],
32
+ arity: 'none',
33
+ class: 'carried',
34
+ why: 'makes the bypass mode available without starting in it',
35
+ },
36
+ { names: ['--allowedTools', '--allowed-tools'], arity: 'variadic', class: 'carried', why: 'tools allowed' },
37
+ { names: ['--append-system-prompt'], arity: 'one', class: 'carried', why: 'appended to the system prompt' },
38
+ { names: ['--autocompact'], arity: 'one', class: 'carried', why: 'the auto-compact window' },
39
+ { names: ['--ax-screen-reader'], arity: 'none', class: 'carried', why: 'screen-reader friendly rendering' },
40
+ {
41
+ names: ['--bg', '--background'],
42
+ arity: 'none',
43
+ class: 'elsewhere',
44
+ why: 'the session starts in the background, not in this terminal',
45
+ },
46
+ { names: ['--bare'], arity: 'none', class: 'rejected', why: HOOKS_OFF },
47
+ { names: ['--betas'], arity: 'variadic', class: 'carried', why: 'API beta headers' },
48
+ { names: ['--brief'], arity: 'none', class: 'carried', why: 'enables the SendUserMessage tool' },
49
+ { names: ['--chrome'], arity: 'none', class: 'carried', why: 'the Claude in Chrome integration' },
50
+ { names: ['--client-data-url'], arity: 'one', class: 'carried', why: 'a signed configuration document' },
51
+ { names: ['--cloud'], arity: 'optional', class: 'elsewhere', why: 'the session runs in the cloud' },
52
+ {
53
+ names: ['-c', '--continue'],
54
+ arity: 'none',
55
+ class: 'mapped',
56
+ maps: 'resume',
57
+ why: 'continues the latest conversation',
58
+ },
59
+ {
60
+ names: ['--dangerously-skip-permissions'],
61
+ arity: 'none',
62
+ class: 'carried',
63
+ why: 'the bypass mode; Claude puts it before any --permission-mode',
64
+ },
65
+ { names: ['-d', '--debug'], arity: 'optional', class: 'carried', why: 'debug mode and its filter' },
66
+ { names: ['--debug-file'], arity: 'one', class: 'carried', why: 'where debug logs go' },
67
+ { names: ['--disable-slash-commands'], arity: 'none', class: 'carried', why: 'disables skills' },
68
+ { names: ['--disallowedTools', '--disallowed-tools'], arity: 'variadic', class: 'carried', why: 'tools denied' },
69
+ { names: ['--effort'], arity: 'one', class: 'mapped', maps: 'effort', why: 'the effort level' },
70
+ {
71
+ names: ['--environment'],
72
+ arity: 'one',
73
+ class: 'elsewhere',
74
+ why: 'the session runs in a cloud environment',
75
+ },
76
+ {
77
+ names: ['--exclude-dynamic-system-prompt-sections'],
78
+ arity: 'none',
79
+ class: 'carried',
80
+ why: 'moves per-machine sections out of the system prompt',
81
+ },
82
+ { names: ['--fallback-model'], arity: 'one', class: 'carried', why: 'models to fall back to' },
83
+ { names: ['--file'], arity: 'variadic', class: 'carried', why: 'files downloaded at startup' },
84
+ {
85
+ names: ['--fork-session'],
86
+ arity: 'none',
87
+ class: 'rejected',
88
+ why: 'it gives the resumed conversation a new session id, and the managed launch owns the session id',
89
+ },
90
+ { names: ['--forward-subagent-text'], arity: 'none', class: 'print-only', why: PRINT_ONLY },
91
+ { names: ['--from-pr'], arity: 'optional', class: 'mapped', maps: 'resume', why: 'resumes the session of a PR' },
92
+ { names: ['-h', '--help'], arity: 'none', class: 'utility', why: 'it prints help', wholeTokenOnly: true },
93
+ { names: ['--ide'], arity: 'none', class: 'carried', why: 'connects to an IDE' },
94
+ { names: ['--include-hook-events'], arity: 'none', class: 'print-only', why: PRINT_ONLY },
95
+ { names: ['--include-partial-messages'], arity: 'none', class: 'print-only', why: PRINT_ONLY },
96
+ {
97
+ names: ['--input-format'],
98
+ arity: 'one',
99
+ class: 'print-only',
100
+ why: PRINT_ONLY,
101
+ choices: ['text', 'stream-json'],
102
+ },
103
+ { names: ['--json-schema'], arity: 'one', class: 'print-only', why: PRINT_ONLY },
104
+ { names: ['--max-budget-usd'], arity: 'one', class: 'print-only', why: PRINT_ONLY },
105
+ { names: ['--mcp-config'], arity: 'variadic', class: 'carried', why: 'MCP servers for the session' },
106
+ { names: ['--model'], arity: 'one', class: 'mapped', maps: 'model', why: 'the model' },
107
+ {
108
+ names: ['-n', '--name'],
109
+ arity: 'one',
110
+ class: 'mapped',
111
+ maps: 'title',
112
+ why: "the session's name; it wins over the host's title",
113
+ },
114
+ { names: ['--no-chrome'], arity: 'none', class: 'carried', why: 'disables the Claude in Chrome integration' },
115
+ { names: ['--no-session-persistence'], arity: 'none', class: 'print-only', why: PRINT_ONLY },
116
+ {
117
+ names: ['--output-format'],
118
+ arity: 'one',
119
+ class: 'print-only',
120
+ why: PRINT_ONLY,
121
+ choices: ['text', 'json', 'stream-json'],
122
+ },
123
+ {
124
+ names: ['--permission-mode'],
125
+ arity: 'one',
126
+ class: 'mapped',
127
+ maps: 'permissionMode',
128
+ why: 'the permission mode',
129
+ // The help lists `manual` where the parser also takes `default`, its former name.
130
+ choices: ['acceptEdits', 'auto', 'bypassPermissions', 'default', 'dontAsk', 'manual', 'plan'],
131
+ },
132
+ {
133
+ names: ['--permission-prompts'],
134
+ arity: 'one',
135
+ class: 'print-only',
136
+ why: PRINT_ONLY,
137
+ choices: ['host', 'none'],
138
+ },
139
+ { names: ['--plugin-dir'], arity: 'one', class: 'carried', why: 'a plugin for this session' },
140
+ { names: ['--plugin-url'], arity: 'one', class: 'carried', why: 'a plugin fetched for this session' },
141
+ { names: ['-p', '--print'], arity: 'none', class: 'one-shot', why: 'it prints one response and exits' },
142
+ {
143
+ names: ['--prompt-suggestions'],
144
+ arity: 'optional',
145
+ class: 'print-only',
146
+ why: PRINT_ONLY,
147
+ choices: ['true', 'false', '1', '0', 'yes', 'no', 'on', 'off'],
148
+ },
149
+ { names: ['--remote-control'], arity: 'optional', class: 'carried', why: 'Remote Control for this session' },
150
+ {
151
+ names: ['--remote-control-session-name-prefix'],
152
+ arity: 'one',
153
+ class: 'carried',
154
+ why: 'the Remote Control session name prefix',
155
+ },
156
+ { names: ['--replay-user-messages'], arity: 'none', class: 'print-only', why: PRINT_ONLY },
157
+ {
158
+ names: ['--restricted'],
159
+ arity: 'none',
160
+ class: 'carried',
161
+ why: 'restricted mode; it keeps --settings, so the managed hooks still fire',
162
+ readsNoSettingsFiles: true,
163
+ },
164
+ { names: ['-r', '--resume'], arity: 'optional', class: 'mapped', maps: 'resume', why: 'resumes a conversation' },
165
+ { names: ['--safe-mode'], arity: 'none', class: 'rejected', why: HOOKS_OFF },
166
+ { names: ['--session-id'], arity: 'one', class: 'rejected', why: SESSION_ID_OWNED },
167
+ { names: ['--setting-sources'], arity: 'one', class: 'mapped', maps: 'settingSources', why: 'the settings layers' },
168
+ {
169
+ names: ['--settings'],
170
+ arity: 'one',
171
+ class: 'rejected',
172
+ why: 'the managed launch passes its own --settings, and a second one would replace it',
173
+ },
174
+ { names: ['--strict-mcp-config'], arity: 'none', class: 'carried', why: 'only the --mcp-config servers' },
175
+ { names: ['--system-prompt'], arity: 'one', class: 'carried', why: 'the system prompt' },
176
+ {
177
+ names: ['--system-prompt-snapshot'],
178
+ arity: 'one',
179
+ class: 'carried',
180
+ why: 'whether the system prompt is recorded',
181
+ choices: ['on', 'off'],
182
+ },
183
+ {
184
+ names: ['--teleport'],
185
+ arity: 'optional',
186
+ class: 'rejected',
187
+ why: "it brings a cloud session into this terminal under that session's own id, which the managed launch cannot own",
188
+ },
189
+ { names: ['--tmux'], arity: 'none', class: 'rejected', why: 'it only works together with --worktree' },
190
+ { names: ['--tools'], arity: 'variadic', class: 'carried', why: 'the built-in tools available' },
191
+ { names: ['--verbose'], arity: 'none', class: 'carried', why: 'verbose output' },
192
+ { names: ['-v', '--version'], arity: 'none', class: 'utility', why: 'it prints the version' },
193
+ {
194
+ names: ['-w', '--worktree'],
195
+ arity: 'optional',
196
+ class: 'rejected',
197
+ why: 'it moves the session into a new worktree, away from the directory this terminal is managed in',
198
+ },
199
+ // Accepted by the CLI but not listed by its help.
200
+ {
201
+ names: ['-d2e', '--debug-to-stderr'],
202
+ arity: 'none',
203
+ class: 'carried',
204
+ why: 'debug mode, to stderr',
205
+ hidden: 'a hidden root option, "(deprecated) Enable debug mode (to stderr)"; parse probe',
206
+ },
207
+ {
208
+ names: ['--system-prompt-file'],
209
+ arity: 'one',
210
+ class: 'carried',
211
+ why: 'the system prompt, from a file',
212
+ hidden: "named by the help's own --bare text; a hidden root option; parse probe",
213
+ },
214
+ {
215
+ names: ['--append-system-prompt-file'],
216
+ arity: 'one',
217
+ class: 'carried',
218
+ why: 'appended to the system prompt, from a file',
219
+ hidden: "named by the help's own --bare text; a hidden root option; parse probe",
220
+ },
221
+ {
222
+ names: ['--max-turns'],
223
+ arity: 'one',
224
+ class: 'print-only',
225
+ why: PRINT_ONLY,
226
+ hidden: 'a hidden root option, "(only works with --print)"; parse probe',
227
+ },
228
+ {
229
+ names: ['--permission-prompt-tool'],
230
+ arity: 'one',
231
+ class: 'print-only',
232
+ why: PRINT_ONLY,
233
+ hidden: 'named by the help\'s own --permission-prompts text; a hidden root option, "(only works with --print)"; parse probe',
234
+ },
235
+ {
236
+ names: ['--remote'],
237
+ arity: 'optional',
238
+ class: 'elsewhere',
239
+ why: 'the session runs in the cloud',
240
+ hidden: 'a hidden root option, "Deprecated alias for --cloud"; parse probe',
241
+ },
242
+ {
243
+ names: ['--pool'],
244
+ arity: 'one',
245
+ class: 'elsewhere',
246
+ why: 'the session runs in a cloud environment',
247
+ hidden: 'a hidden root option, "Deprecated alias for --environment"; parse probe',
248
+ },
249
+ {
250
+ names: ['--rc'],
251
+ arity: 'optional',
252
+ class: 'carried',
253
+ why: 'Remote Control for this session',
254
+ hidden: 'a hidden root option, "Alias for --remote-control"; parse probe',
255
+ },
256
+ ];
257
+ const BACKGROUND_HOUSEKEEPING = 'it manages background sessions and starts none here';
258
+ /** Every subcommand the CLI dispatches at {@link CLAUDE_INVOCATION_GRAMMAR_VERSION}, in the help's order. */
259
+ export const CLAUDE_SUBCOMMANDS = [
260
+ { names: ['agents'], class: 'utility', why: BACKGROUND_HOUSEKEEPING, dispatch: ['command'] },
261
+ {
262
+ names: ['attach'],
263
+ class: 'elsewhere',
264
+ why: 'it opens a background session that runs outside this terminal',
265
+ dispatch: ['first-argument'],
266
+ },
267
+ { names: ['auth'], class: 'utility', why: 'it manages authentication', dispatch: ['command'] },
268
+ { names: ['auto-mode'], class: 'utility', why: 'it manages the auto mode configuration', dispatch: ['command'] },
269
+ { names: ['doctor'], class: 'utility', why: 'it checks the installation', dispatch: ['command'] },
270
+ { names: ['gateway'], class: 'utility', why: 'it runs the enterprise gateway', dispatch: ['command'] },
271
+ // Bare, or with only its own choices, `import` is rewritten into a session that runs /import
272
+ // before the parser sees it (CLAUDE_IMPORT_REWRITE); any other form reaches this subcommand.
273
+ { names: ['import'], class: 'utility', why: 'it prints how to import configuration', dispatch: ['command'] },
274
+ { names: ['install'], class: 'utility', why: 'it installs Claude Code', dispatch: ['command'] },
275
+ { names: ['logs'], class: 'utility', why: BACKGROUND_HOUSEKEEPING, dispatch: ['first-argument'] },
276
+ { names: ['mcp'], class: 'utility', why: 'it manages MCP servers', dispatch: ['command'] },
277
+ { names: ['plugin', 'plugins'], class: 'utility', why: 'it manages plugins', dispatch: ['command'] },
278
+ { names: ['project'], class: 'utility', why: 'it manages project state', dispatch: ['command'] },
279
+ { names: ['respawn'], class: 'utility', why: BACKGROUND_HOUSEKEEPING, dispatch: ['first-argument'] },
280
+ { names: ['rm'], class: 'utility', why: BACKGROUND_HOUSEKEEPING, dispatch: ['first-argument'] },
281
+ { names: ['setup-token'], class: 'utility', why: 'it sets up a token', dispatch: ['command'] },
282
+ { names: ['stop', 'kill'], class: 'utility', why: BACKGROUND_HOUSEKEEPING, dispatch: ['first-argument'] },
283
+ { names: ['ultrareview'], class: 'elsewhere', why: 'the review runs in the cloud', dispatch: ['command'] },
284
+ { names: ['update', 'upgrade'], class: 'utility', why: 'it updates Claude Code', dispatch: ['command'] },
285
+ // Accepted by the CLI but not listed by its help.
286
+ {
287
+ names: ['remote-control', 'rc'],
288
+ class: 'elsewhere',
289
+ why: 'Remote Control runs its sessions outside this terminal',
290
+ dispatch: ['first-argument', 'command'],
291
+ hidden: 'a hidden subcommand and an entry dispatch; the CLI says root options are "not carried over to the sessions Remote Control starts"',
292
+ },
293
+ {
294
+ names: ['remote'],
295
+ class: 'elsewhere',
296
+ why: 'Remote Control runs its sessions outside this terminal',
297
+ dispatch: ['first-argument'],
298
+ hidden: 'an entry dispatch to Remote Control, as the first argument only',
299
+ },
300
+ {
301
+ names: ['sync'],
302
+ class: 'elsewhere',
303
+ why: 'Remote Control runs its sessions outside this terminal',
304
+ dispatch: ['first-argument'],
305
+ hidden: 'an entry dispatch to Remote Control, as the first argument only',
306
+ },
307
+ {
308
+ names: ['bridge'],
309
+ class: 'elsewhere',
310
+ why: 'Remote Control runs its sessions outside this terminal',
311
+ dispatch: ['first-argument'],
312
+ hidden: 'an entry dispatch to Remote Control, as the first argument only',
313
+ },
314
+ {
315
+ names: ['self-hosted-runner'],
316
+ class: 'elsewhere',
317
+ why: 'the runner runs cloud sessions outside this terminal',
318
+ dispatch: ['first-argument'],
319
+ hidden: 'an entry dispatch, as the first argument only; the runner starts its sessions as --print --sdk-url children',
320
+ },
321
+ {
322
+ names: ['daemon'],
323
+ class: 'utility',
324
+ why: 'it manages the background-session daemon',
325
+ dispatch: ['after-skip-permission-flags'],
326
+ hidden: 'an entry dispatch (run, stop, status, install, uninstall)',
327
+ },
328
+ {
329
+ names: ['sandbox'],
330
+ class: 'utility',
331
+ why: 'it manages the sandbox',
332
+ dispatch: ['command'],
333
+ hidden: 'a hidden subcommand (install, status)',
334
+ },
335
+ {
336
+ names: ['import-conversations'],
337
+ class: 'utility',
338
+ why: 'it imports conversations into the archive',
339
+ dispatch: ['command'],
340
+ hidden: 'a hidden subcommand',
341
+ },
342
+ ...[
343
+ 'edit-hook',
344
+ 'edit-permission-rules',
345
+ 'edit-memory-settings',
346
+ 'edit-skill-overrides',
347
+ 'edit-sandbox-settings',
348
+ 'edit-chrome-settings',
349
+ 'design-login',
350
+ ].map((name) => ({
351
+ names: [name],
352
+ class: 'utility',
353
+ why: 'the VS Code extension runs it; it starts no session',
354
+ dispatch: ['command'],
355
+ hidden: 'a hidden subcommand "used by the VS Code extension"',
356
+ })),
357
+ ];
358
+ /** In the CLI's own order: the first that matches decides. */
359
+ export const CLAUDE_ENTRY_RULES = [
360
+ {
361
+ id: 'version-alone',
362
+ match: {
363
+ argvIs: ['--version', '-v', '-V'].flatMap((flag) => [[flag], [flag, '--verbose']]),
364
+ },
365
+ class: 'utility',
366
+ why: 'it prints the version',
367
+ },
368
+ { id: 'plugin-test', match: { firstTwo: ['plugin', 'test'] }, class: 'utility', why: 'it tests a plugin' },
369
+ { id: 'first-argument', match: { subcommand: 'first-argument' } },
370
+ { id: 'after-skip-permission-flags', match: { subcommand: 'after-skip-permission-flags' } },
371
+ {
372
+ id: 'background',
373
+ match: { anywhere: ['--bg', '--background'] },
374
+ class: 'elsewhere',
375
+ why: 'the session starts in the background, not in this terminal',
376
+ },
377
+ {
378
+ id: 'tmux-worktree',
379
+ match: {
380
+ anywhere: ['--tmux', '--tmux=classic'],
381
+ also: { tokens: ['-w', '--worktree'], prefixes: ['--worktree='] },
382
+ },
383
+ class: 'elsewhere',
384
+ why: 'the session runs in a tmux session the CLI creates for the worktree',
385
+ },
386
+ {
387
+ id: 'update-alone',
388
+ match: { argvIs: [['--update'], ['--upgrade']] },
389
+ class: 'utility',
390
+ why: 'it updates Claude Code',
391
+ },
392
+ {
393
+ id: 'deep-link',
394
+ match: { withNext: '--handle-uri' },
395
+ class: 'rejected',
396
+ why: 'it opens a deep link, which this version of the Claude adapter does not know',
397
+ },
398
+ ];
399
+ /**
400
+ * Checked on the raw argv before the first `--`, whatever the parser makes of the token (even the
401
+ * value of another option): the CLI turns hooks off from them before it parses.
402
+ */
403
+ export const CLAUDE_BLINDING_TOKENS = ['--bare', '--safe-mode'];
404
+ /**
405
+ * In Claude Code 2.1.283 each is read through one parser, whatever reads it (findings.md §7.5):
406
+ * CLAUDE_CODE_SIMPLE does what --bare does (no hook fires: live F01), CLAUDE_CODE_SAFE_MODE what
407
+ * --safe-mode does (none fires: F03), and CLAUDE_CODE_RESTRICTED what --restricted does (the
408
+ * managed hooks fire, the person's settings files are not read: F04).
409
+ */
410
+ export const CLAUDE_ENVIRONMENT_SWITCHES = [
411
+ { name: 'CLAUDE_CODE_SIMPLE', flag: '--bare', class: 'rejected', why: HOOKS_OFF },
412
+ { name: 'CLAUDE_CODE_SAFE_MODE', flag: '--safe-mode', class: 'rejected', why: HOOKS_OFF },
413
+ {
414
+ name: 'CLAUDE_CODE_RESTRICTED',
415
+ flag: '--restricted',
416
+ class: 'carried',
417
+ why: 'restricted mode; it keeps --settings, so the managed hooks still fire',
418
+ readsNoSettingsFiles: true,
419
+ },
420
+ ];
421
+ /** The CLI's own reading of a switch: on for 1, true, yes or on, in any case, trimmed; off for anything else. */
422
+ export function claudeSwitchIsOn(value) {
423
+ return value !== undefined && ['1', 'true', 'yes', 'on'].includes(value.toLowerCase().trim());
424
+ }
425
+ export function readClaudeEnvironment(environment) {
426
+ let readsNoSettingsFiles = false;
427
+ for (const row of CLAUDE_ENVIRONMENT_SWITCHES) {
428
+ const value = environment[row.name];
429
+ if (!claudeSwitchIsOn(value))
430
+ continue;
431
+ if (row.class === 'rejected') {
432
+ return { refusal: { reason: row.why, argument: `${row.name}=${value}` }, readsNoSettingsFiles };
433
+ }
434
+ if (row.readsNoSettingsFiles === true)
435
+ readsNoSettingsFiles = true;
436
+ }
437
+ return { readsNoSettingsFiles };
438
+ }
439
+ /**
440
+ * `claude [options] import [codex|gemini|cursor] [--dry-run] [--yes[=<digest>]]` is rewritten to
441
+ * `claude [options] -- "/import …"` before the parser runs: an interactive session that runs
442
+ * /import (when the CLI has the feature on; otherwise it exits with an error).
443
+ */
444
+ export const CLAUDE_IMPORT_REWRITE = {
445
+ token: 'import',
446
+ /** None of these may come before it. */
447
+ prefixExcludes: ['--', '-h', '--help', '-v', '-V', '--version'],
448
+ /** Everything after it must be one of these. */
449
+ rest: ['codex', 'gemini', 'cursor', '--dry-run', '--yes'],
450
+ restPrefixes: ['--yes='],
451
+ };
452
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
453
+ const SETTING_SOURCES = new Set(['user', 'project', 'local']);
454
+ const SKIP_PERMISSION_FLAGS = new Set(['--dangerously-skip-permissions', '--allow-dangerously-skip-permissions']);
455
+ const OPTIONS_BY_NAME = new Map(CLAUDE_OPTIONS.flatMap((row) => row.names.map((name) => [name, row])));
456
+ const SUBCOMMANDS_BY_NAME = new Map(CLAUDE_SUBCOMMANDS.flatMap((row) => row.names.map((name) => [name, row])));
457
+ /** Commander's test for a token that is an option: a lone `-` is an operand. */
458
+ function looksLikeOption(token) {
459
+ return token.length > 1 && token.startsWith('-');
460
+ }
461
+ function quoted(token) {
462
+ return `\`${token}\``;
463
+ }
464
+ /** The reading one entry rule gives the raw argv, or `undefined` when it does not match. */
465
+ function entryReading(rule, argv) {
466
+ const match = rule.match;
467
+ if ('subcommand' in match) {
468
+ const position = match.subcommand === 'first-argument' ? argv[0] : argv.find((token) => !SKIP_PERMISSION_FLAGS.has(token));
469
+ const row = position === undefined ? undefined : SUBCOMMANDS_BY_NAME.get(position);
470
+ return row?.dispatch.includes(match.subcommand) ? passthrough(row.class, row.why) : undefined;
471
+ }
472
+ let matched;
473
+ if ('argvIs' in match) {
474
+ matched = match.argvIs.some((shape) => shape.length === argv.length && shape.every((token, index) => argv[index] === token));
475
+ }
476
+ else if ('firstTwo' in match) {
477
+ matched = argv[0] === match.firstTwo[0] && argv[1] === match.firstTwo[1];
478
+ }
479
+ else if ('withNext' in match) {
480
+ const index = argv.indexOf(match.withNext);
481
+ matched = index !== -1 && argv[index + 1] !== undefined;
482
+ }
483
+ else {
484
+ const also = match.also;
485
+ matched =
486
+ argv.some((token) => match.anywhere.includes(token)) &&
487
+ (also === undefined ||
488
+ argv.some((token) => also.tokens.includes(token) || also.prefixes.some((prefix) => token.startsWith(prefix))));
489
+ }
490
+ if (!matched)
491
+ return undefined;
492
+ if (rule.class === undefined || rule.why === undefined) {
493
+ throw new Error(`Claude entry rule ${rule.id} answers with no class of its own`);
494
+ }
495
+ if (rule.class !== 'rejected')
496
+ return passthrough(rule.class, rule.why);
497
+ return { status: 'rejected', reason: rule.why, ...('withNext' in match ? { argument: match.withNext } : {}) };
498
+ }
499
+ /** The entry's dispatch before its parser: the first rule that matches. */
500
+ function entryDispatch(argv) {
501
+ for (const rule of CLAUDE_ENTRY_RULES) {
502
+ const reading = entryReading(rule, argv);
503
+ if (reading !== undefined)
504
+ return reading;
505
+ }
506
+ return undefined;
507
+ }
508
+ function passthrough(kind, reason) {
509
+ return { status: 'passthrough', kind, reason };
510
+ }
511
+ /** The CLI's own import rewrite, applied to the raw argv; `undefined` when it does not apply. */
512
+ function importRewrite(argv) {
513
+ const index = argv.indexOf(CLAUDE_IMPORT_REWRITE.token);
514
+ if (index === -1)
515
+ return undefined;
516
+ const prefix = argv.slice(0, index);
517
+ const rest = argv.slice(index + 1);
518
+ if (prefix.some((token) => CLAUDE_IMPORT_REWRITE.prefixExcludes.includes(token))) {
519
+ return undefined;
520
+ }
521
+ if (prefix.length > 0 && rest.length === 0)
522
+ return undefined;
523
+ if (!prefix.every((token, position) => token.startsWith('-') || prefix[position - 1]?.startsWith('-')))
524
+ return undefined;
525
+ const allowed = (token) => CLAUDE_IMPORT_REWRITE.rest.includes(token) ||
526
+ CLAUDE_IMPORT_REWRITE.restPrefixes.some((restPrefix) => token.startsWith(restPrefix));
527
+ if (!rest.every(allowed))
528
+ return undefined;
529
+ return [...prefix, '--', ['/import', ...rest].join(' ')];
530
+ }
531
+ /**
532
+ * Whether the argv asks for print mode the way the CLI decides it before registering subcommands:
533
+ * an exact `-p`/`--print` token, before `--`, that is not an option's value (a value is skipped by
534
+ * the same arities the parser uses; clusters and `--flag=value` tokens are not split). The same
535
+ * scan finds a `cc://` address, which registers subcommands even in print mode.
536
+ */
537
+ function scanBeforeParse(argv) {
538
+ let print = false;
539
+ let connect = false;
540
+ for (let index = 0; index < argv.length; index += 1) {
541
+ const token = argv[index];
542
+ if (token === '--')
543
+ break;
544
+ if (token === '-p' || token === '--print')
545
+ print = true;
546
+ if (token.startsWith('cc://') || token.startsWith('cc+unix://'))
547
+ connect = true;
548
+ const row = OPTIONS_BY_NAME.get(token);
549
+ if (row === undefined || row.arity === 'none')
550
+ continue;
551
+ const next = argv[index + 1];
552
+ if (row.arity === 'optional') {
553
+ if (next !== undefined && !looksLikeOption(next))
554
+ index += 1;
555
+ continue;
556
+ }
557
+ index += 1;
558
+ if (row.arity === 'variadic') {
559
+ while (argv[index + 1] !== undefined && !looksLikeOption(argv[index + 1]))
560
+ index += 1;
561
+ }
562
+ }
563
+ return { print, connect };
564
+ }
565
+ /** Commander's parse of the root command, with the table's rows as its options. */
566
+ class ClaudeCommandLineReader {
567
+ pending;
568
+ subcommandsRegistered;
569
+ operands = [];
570
+ carriedArgs = [];
571
+ /** In argv order; print-only rows among them refuse only when no `-p` makes the invocation a one-shot. */
572
+ refusals = [];
573
+ mapped = {};
574
+ firstCarried;
575
+ readsNoSettingsFiles = false;
576
+ print = false;
577
+ elsewhere;
578
+ constructor(argv) {
579
+ this.pending = [...argv];
580
+ const scanned = scanBeforeParse(argv);
581
+ this.subcommandsRegistered = !scanned.print || scanned.connect;
582
+ }
583
+ read() {
584
+ let endOfOptions = false;
585
+ let variadic;
586
+ while (this.pending.length > 0) {
587
+ const token = this.pending.shift();
588
+ if (endOfOptions) {
589
+ const decided = this.operand(token);
590
+ if (decided !== undefined)
591
+ return decided;
592
+ continue;
593
+ }
594
+ if (token === '--') {
595
+ endOfOptions = true;
596
+ variadic = undefined;
597
+ continue;
598
+ }
599
+ if (variadic !== undefined && !looksLikeOption(token)) {
600
+ if (variadic.class === 'carried')
601
+ this.carriedArgs.push(token);
602
+ continue;
603
+ }
604
+ variadic = undefined;
605
+ if (!looksLikeOption(token)) {
606
+ const decided = this.operand(token);
607
+ if (decided !== undefined)
608
+ return decided;
609
+ continue;
610
+ }
611
+ const exact = OPTIONS_BY_NAME.get(token);
612
+ if (exact !== undefined) {
613
+ const decided = this.option(exact, this.take(exact, token));
614
+ if (decided !== undefined)
615
+ return decided;
616
+ if (exact.arity === 'variadic')
617
+ variadic = exact;
618
+ continue;
619
+ }
620
+ // A cluster: a flag at its head leaves `-<rest>` to parse next; a head that takes a value
621
+ // takes the rest of the token as its value.
622
+ if (token[1] !== '-' && token.length > 2) {
623
+ const spelling = `-${token[1]}`;
624
+ const head = OPTIONS_BY_NAME.get(spelling);
625
+ if (head !== undefined && head.wholeTokenOnly !== true) {
626
+ const flag = head.arity === 'none';
627
+ const decided = this.option(head, flag ? { tokens: [spelling], spelling } : { tokens: [token], spelling, value: token.slice(2) });
628
+ if (decided !== undefined)
629
+ return decided;
630
+ if (flag)
631
+ this.pending.unshift(`-${token.slice(2)}`);
632
+ continue;
633
+ }
634
+ }
635
+ if (/^--[^=]+=/u.test(token)) {
636
+ const spelling = token.slice(0, token.indexOf('='));
637
+ const named = OPTIONS_BY_NAME.get(spelling);
638
+ if (named !== undefined && named.arity !== 'none') {
639
+ const decided = this.option(named, { tokens: [token], spelling, value: token.slice(spelling.length + 1) });
640
+ if (decided !== undefined)
641
+ return decided;
642
+ continue;
643
+ }
644
+ }
645
+ return this.unknown(token);
646
+ }
647
+ return this.settle();
648
+ }
649
+ /** The first operand selects a registered subcommand, even after `--`; positional options give it the rest. */
650
+ operand(token) {
651
+ if (this.operands.length === 0 && this.subcommandsRegistered) {
652
+ const subcommand = SUBCOMMANDS_BY_NAME.get(token);
653
+ if (subcommand?.dispatch.includes('command'))
654
+ return passthrough(subcommand.class, subcommand.why);
655
+ }
656
+ this.operands.push(token);
657
+ return undefined;
658
+ }
659
+ /** The value an option given as a whole token takes from the tokens after it. */
660
+ take(row, token) {
661
+ if (row.arity === 'none')
662
+ return { tokens: [token], spelling: token };
663
+ const next = this.pending[0];
664
+ if (row.arity === 'optional') {
665
+ if (next === undefined || looksLikeOption(next))
666
+ return { tokens: [token], spelling: token };
667
+ }
668
+ else if (next === undefined) {
669
+ return undefined;
670
+ }
671
+ this.pending.shift();
672
+ return { tokens: [token, next], spelling: token, value: next };
673
+ }
674
+ /**
675
+ * One option and what it took (`undefined`: a value it needs is missing). Returns a reading only
676
+ * when the option decides the invocation there and then: help and version exit while commander parses.
677
+ */
678
+ option(row, taken) {
679
+ const spelling = taken?.spelling ?? row.names.at(-1);
680
+ if (taken === undefined) {
681
+ this.refusals.push({ reason: 'it needs a value', argument: spelling });
682
+ return undefined;
683
+ }
684
+ if (taken.value !== undefined && row.choices !== undefined && !row.choices.includes(taken.value)) {
685
+ const choices = `${row.choices.slice(0, -1).join(', ')} or ${row.choices.at(-1)}`;
686
+ this.refusals.push({ reason: `it takes ${choices}, not ${JSON.stringify(taken.value)}`, argument: spelling });
687
+ return undefined;
688
+ }
689
+ switch (row.class) {
690
+ case 'utility':
691
+ return passthrough('utility', row.why);
692
+ case 'elsewhere':
693
+ this.elsewhere ??= row;
694
+ return undefined;
695
+ case 'one-shot':
696
+ this.print = true;
697
+ return undefined;
698
+ case 'print-only':
699
+ case 'rejected':
700
+ this.refusals.push({ reason: row.why, argument: spelling });
701
+ return undefined;
702
+ case 'carried':
703
+ this.carriedArgs.push(...taken.tokens);
704
+ this.firstCarried ??= spelling;
705
+ if (row.readsNoSettingsFiles === true)
706
+ this.readsNoSettingsFiles = true;
707
+ return undefined;
708
+ case 'mapped':
709
+ this.map(row, taken);
710
+ return undefined;
711
+ }
712
+ }
713
+ map(row, taken) {
714
+ const refuse = (reason) => this.refusals.push({ reason, argument: taken.spelling });
715
+ switch (row.maps) {
716
+ case 'resume':
717
+ if (this.mapped.resumeInvocation !== undefined)
718
+ refuse('it resumes a conversation a second way');
719
+ else
720
+ this.mapped.resumeInvocation = [...taken.tokens];
721
+ return;
722
+ case 'settingSources': {
723
+ const sources = taken.value.split(',');
724
+ if (this.mapped.settingSources !== undefined)
725
+ refuse('it is given more than once');
726
+ else if (sources.some((source) => !SETTING_SOURCES.has(source))) {
727
+ refuse('it takes user, project and local, comma-separated');
728
+ }
729
+ else
730
+ this.mapped.settingSources = sources;
731
+ return;
732
+ }
733
+ case 'model':
734
+ case 'effort':
735
+ case 'permissionMode':
736
+ case 'title':
737
+ if (this.mapped[row.maps] !== undefined)
738
+ refuse('it is given more than once');
739
+ else
740
+ this.mapped[row.maps] = taken.value;
741
+ return;
742
+ default:
743
+ throw new Error(`mapped Claude option ${taken.spelling} names no managed option`);
744
+ }
745
+ }
746
+ /** A token that starts with `-` and has no row: its arity cannot be known, so nothing after it can be read. */
747
+ unknown(token) {
748
+ const established = this.passthroughSoFar();
749
+ if (established !== undefined)
750
+ return established;
751
+ const first = this.refusals[0];
752
+ if (first !== undefined)
753
+ return { status: 'rejected', reason: first.reason, argument: first.argument };
754
+ return {
755
+ status: 'rejected',
756
+ reason: `this version of the Claude adapter (Claude Code ${CLAUDE_INVOCATION_GRAMMAR_VERSION}) does not know it`,
757
+ argument: token,
758
+ };
759
+ }
760
+ passthroughSoFar() {
761
+ if (this.elsewhere !== undefined)
762
+ return passthrough('elsewhere', this.elsewhere.why);
763
+ if (this.print)
764
+ return passthrough('one-shot', 'it prints one response and exits');
765
+ return undefined;
766
+ }
767
+ settle() {
768
+ const established = this.passthroughSoFar();
769
+ if (established !== undefined)
770
+ return established;
771
+ const refusal = this.refusals[0];
772
+ if (refusal !== undefined)
773
+ return { status: 'rejected', reason: refusal.reason, argument: refusal.argument };
774
+ const resumeInvocation = this.mapped.resumeInvocation;
775
+ const resumeValue = resumeInvocation === undefined ? undefined : resumeTarget(resumeInvocation);
776
+ return {
777
+ status: 'accepted',
778
+ ...(this.mapped.model === undefined ? {} : { model: this.mapped.model }),
779
+ ...(this.mapped.effort === undefined ? {} : { effort: this.mapped.effort }),
780
+ ...(this.mapped.permissionMode === undefined ? {} : { permissionMode: this.mapped.permissionMode }),
781
+ ...(this.mapped.settingSources === undefined ? {} : { settingSources: this.mapped.settingSources }),
782
+ ...(this.mapped.title === undefined ? {} : { title: this.mapped.title }),
783
+ ...(resumeValue !== undefined && UUID.test(resumeValue)
784
+ ? { resume: resumeValue }
785
+ : resumeInvocation === undefined
786
+ ? {}
787
+ : { resumeInvocation }),
788
+ carriedArgs: this.carriedArgs,
789
+ prompt: this.operands,
790
+ ...(this.firstCarried === undefined ? {} : { firstCarried: this.firstCarried }),
791
+ readsNoSettingsFiles: this.readsNoSettingsFiles,
792
+ };
793
+ }
794
+ }
795
+ /** The value a `-r`/`--resume` form names, when it names one (a picker search term or an id). */
796
+ function resumeTarget(tokens) {
797
+ const [first, second] = tokens;
798
+ if (first === '-r' || first === '--resume')
799
+ return second;
800
+ if (first?.startsWith('--resume='))
801
+ return first.slice('--resume='.length);
802
+ if (first?.startsWith('-r') && first.length > 2 && !first.startsWith('--'))
803
+ return first.slice(2);
804
+ return undefined;
805
+ }
806
+ /** The argv the managed launch would build, with placeholder values where the recipe puts its own. */
807
+ function managedShape(session) {
808
+ return [
809
+ ...(session.resumeInvocation ??
810
+ (session.resume === undefined ? ['--session-id', 'id'] : ['--resume', session.resume])),
811
+ ...(session.title === undefined ? [] : ['--name', session.title]),
812
+ ...(session.model === undefined ? [] : ['--model', session.model]),
813
+ ...(session.effort === undefined ? [] : ['--effort', session.effort]),
814
+ ...(session.settingSources === undefined ? [] : ['--setting-sources', session.settingSources.join(',')]),
815
+ '--settings',
816
+ 'settings.json',
817
+ ...(session.permissionMode === undefined ? [] : ['--permission-mode', session.permissionMode]),
818
+ ...session.carriedArgs,
819
+ ...(session.prompt.length === 0 ? [] : ['--', ...session.prompt]),
820
+ ];
821
+ }
822
+ /**
823
+ * Read a typed `claude …` (the argv after the command name) the way Claude Code
824
+ * {@link CLAUDE_INVOCATION_GRAMMAR_VERSION} reads it.
825
+ */
826
+ export function readClaudeInvocation(argv) {
827
+ const dispatched = entryDispatch(argv);
828
+ if (dispatched !== undefined)
829
+ return dispatched;
830
+ const rewritten = importRewrite(argv);
831
+ const reading = new ClaudeCommandLineReader(rewritten ?? argv).read();
832
+ if (reading.status !== 'accepted')
833
+ return reading;
834
+ const end = argv.indexOf('--');
835
+ const blinding = (end === -1 ? argv : argv.slice(0, end)).find((token) => CLAUDE_BLINDING_TOKENS.includes(token));
836
+ if (blinding !== undefined)
837
+ return { status: 'rejected', reason: HOOKS_OFF, argument: blinding };
838
+ if (rewritten !== undefined) {
839
+ return {
840
+ status: 'rejected',
841
+ reason: 'it starts a session that runs /import, which the managed launch does not carry',
842
+ argument: CLAUDE_IMPORT_REWRITE.token,
843
+ };
844
+ }
845
+ // The managed launch puts its own flags first and the prompt after `--`: the entry must read
846
+ // that argv as the same plain session, or the arguments cannot be passed on as typed.
847
+ const shape = managedShape(reading);
848
+ if (entryDispatch(shape) !== undefined || importRewrite(shape) !== undefined) {
849
+ return {
850
+ status: 'rejected',
851
+ reason: 'the managed launch could not pass these arguments on as typed',
852
+ ...(reading.firstCarried === undefined ? {} : { argument: reading.firstCarried }),
853
+ };
854
+ }
855
+ return reading;
856
+ }
857
+ //# sourceMappingURL=invocation-grammar.js.map