@miphamai/cli 0.85.11 → 0.85.13

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 (38) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/agent/sub-agent.ts +22 -0
  4. package/src/config/loader.ts +101 -6
  5. package/src/core/context-microcompact.ts +3 -5
  6. package/src/core/context.ts +17 -11
  7. package/src/core/credential-masker/output-scrub.ts +7 -1
  8. package/src/core/crsi-sandbox.ts +101 -49
  9. package/src/core/engine.ts +22 -2
  10. package/src/core/hooks-executor.ts +165 -16
  11. package/src/core/permission-classifier.ts +36 -12
  12. package/src/core/permission.ts +45 -0
  13. package/src/core/self-critique.ts +3 -15
  14. package/src/core/task-performance.ts +32 -4
  15. package/src/i18n-core/locales/en-US.json +1 -0
  16. package/src/i18n-core/locales/zh-CN.json +1 -0
  17. package/src/index.tsx +15 -1
  18. package/src/plugin/plugin-loader.ts +10 -9
  19. package/src/providers/anthropic.ts +11 -3
  20. package/src/providers/fetch-utils.ts +29 -0
  21. package/src/providers/openai-compat.ts +24 -2
  22. package/src/providers/registry.ts +38 -0
  23. package/src/security/world-writable-chmod.ts +157 -0
  24. package/src/shared/package-info.ts +2 -2
  25. package/src/shared/sanitize.ts +33 -0
  26. package/src/shared/types.ts +30 -0
  27. package/src/tools/agent/agent.ts +18 -3
  28. package/src/tools/agent/exit-plan.ts +49 -9
  29. package/src/tools/artifact/artifact.ts +0 -1
  30. package/src/tools/exec/bash.ts +28 -3
  31. package/src/tools/index.ts +7 -1
  32. package/src/tools/network/web-fetch.ts +2 -1
  33. package/src/ui/chat.tsx +2 -1
  34. package/src/ui/commands.ts +7 -6
  35. package/src/ui/input.tsx +5 -0
  36. package/src/workflow/event-bus.ts +13 -0
  37. package/src/workflow/journal.ts +8 -0
  38. package/src/workflow/runtime.ts +59 -11
package/src/ui/input.tsx CHANGED
@@ -399,6 +399,11 @@ export function InputBar({
399
399
  useInput((input, key) => {
400
400
  // ── Escape: cancel loading → clear draft ──
401
401
  if (key.escape) {
402
+ // While the command picker is open, Escape belongs to it. Ink 7 runs every
403
+ // registered `useInput` listener for one keypress (no stopPropagation), so
404
+ // without this the picker closes AND this branch wipes the draft — the exact
405
+ // text the picker's `onClose` is written to preserve.
406
+ if (pickerActive) return
402
407
  // Escape while loading → abort
403
408
  if (isLoading) {
404
409
  onCancel?.()
@@ -18,6 +18,19 @@ export class WorkflowEventBus extends EventEmitter {
18
18
  }
19
19
 
20
20
  emitEvent(event: WorkflowEvent): void {
21
+ // `error` here is a **payload type**, not EventEmitter's crash channel — and the
22
+ // two are wired to the same string. Node makes `emit('error', …)` throw
23
+ // ERR_UNHANDLED_ERROR when no listener is attached, so a workflow that failed
24
+ // while nothing was watching the bus replaced its own diagnostic with
25
+ // `Unhandled error. ({ type: 'error', message: '…' })`, and the carefully
26
+ // written `throw new Error('Workflow script execution failed: …')` further down
27
+ // never ran. It looked fine because the UI progress view subscribes to `error`,
28
+ // so the throw only happened headless.
29
+ //
30
+ // Nothing is lost by skipping it: whoever needs the failure has the journal entry
31
+ // and the thrown error; the one subscriber drops `error` on the floor anyway
32
+ // (`workflow-progress.tsx`'s `case 'error': break`).
33
+ if (event.type === 'error' && this.listenerCount('error') === 0) return
21
34
  this.emit(event.type, event)
22
35
  }
23
36
 
@@ -13,6 +13,14 @@ export interface JournalEntry {
13
13
  opts?: Record<string, unknown>
14
14
  result?: unknown
15
15
  message?: string
16
+ /**
17
+ * Why an `agent` entry has no `result`.
18
+ *
19
+ * Kept out of `result` on purpose: the resume cache is keyed on `result !==
20
+ * undefined`, so parking the message there would make a failed call replay as a
21
+ * successful cached answer.
22
+ */
23
+ error?: string
16
24
  }
17
25
 
18
26
  export interface JournalState {
@@ -108,7 +108,7 @@ export async function runWorkflow(
108
108
  const budget = createBudget(budgetTotal)
109
109
 
110
110
  // Wrap primitives with journal recording + cache support
111
- const agent = async (prompt: string, opts?: Record<string, unknown>) => {
111
+ const runAgent = async (prompt: string, opts?: Record<string, unknown>) => {
112
112
  // Check cache first
113
113
  if (resultCache) {
114
114
  const key = agentCacheKey(prompt, opts || {})
@@ -125,16 +125,42 @@ export async function runWorkflow(
125
125
 
126
126
  bus.emitEvent({ type: 'agent:start', agentId, label: agentId, phase })
127
127
 
128
- const result = await workflowAgent(
129
- prompt,
130
- registry,
131
- toolRegistry,
132
- {
133
- ...(opts || {}),
134
- permissionSystem: permission,
135
- } as Record<string, unknown>,
136
- llm,
137
- )
128
+ let result: unknown
129
+ try {
130
+ result = await workflowAgent(
131
+ prompt,
132
+ registry,
133
+ toolRegistry,
134
+ {
135
+ ...(opts || {}),
136
+ permissionSystem: permission,
137
+ } as Record<string, unknown>,
138
+ llm,
139
+ )
140
+ } catch (err) {
141
+ // A failed call is still a call: it gets the same start/end pair and a journal
142
+ // entry, so the run reads as "this agent failed" rather than as a run that
143
+ // silently never mentioned it. The message goes in `error`, not `result` —
144
+ // the resume cache replays on `result !== undefined`.
145
+ const message = err instanceof Error ? err.message : String(err)
146
+ bus.emitEvent({
147
+ type: 'agent:end',
148
+ agentId,
149
+ label: agentId,
150
+ success: false,
151
+ durationMs: Date.now() - startTime,
152
+ })
153
+ bus.emitEvent({ type: 'error', agentId, message })
154
+ appendJournal(runId, {
155
+ type: 'agent',
156
+ prompt,
157
+ opts: opts as Record<string, unknown> | undefined,
158
+ error: message,
159
+ })
160
+ // Still rethrow: a script that awaits this call must see the rejection, and
161
+ // the catch below turns it into the run's own error message.
162
+ throw err
163
+ }
138
164
 
139
165
  const durationMs = Date.now() - startTime
140
166
  bus.emitEvent({ type: 'agent:end', agentId, label: agentId, success: true, durationMs })
@@ -148,6 +174,28 @@ export async function runWorkflow(
148
174
  return result
149
175
  }
150
176
 
177
+ /**
178
+ * The `agent` the sandbox script sees.
179
+ *
180
+ * Not `async`, because the promise it returns needs a handler attached before it
181
+ * leaves this function. A script that calls `agent(...)` **without awaiting it** —
182
+ * a fire-and-forget fan-out, or `agent('A'); await agent('B')` — leaves the
183
+ * rejection unhandled in the host realm, and the crash reporter is installed
184
+ * unconditionally, so `handleFatal` ends the process: one un-awaited call and the
185
+ * CLI exits with a message about unhandled rejections in a workflow script.
186
+ *
187
+ * Attaching the handler here marks the rejection handled **without swallowing it**:
188
+ * the promise returned to the script is the same one, so `await agent(...)` still
189
+ * throws into the script's own `try`. What changes is only the case where nobody is
190
+ * listening — that case now belongs to the run (journal + `error` event, both
191
+ * written by `runAgent`), not to the process.
192
+ */
193
+ const agent = (prompt: string, opts?: Record<string, unknown>) => {
194
+ const pending = runAgent(prompt, opts)
195
+ pending.catch(() => {})
196
+ return pending
197
+ }
198
+
151
199
  const wrappedPhase = (title: string) => {
152
200
  phasePrimitive(title)
153
201
  bus.emitEvent({ type: 'phase:start', phase: title, timestamp: Date.now() })