@miphamai/cli 0.85.16 → 0.85.18
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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/agent/sub-agent.ts +7 -0
- package/src/config/defaults.ts +6 -1
- package/src/core/credential-masker/output-scrub.ts +8 -1
- package/src/core/engine.ts +43 -4
- package/src/core/instructions.ts +20 -1
- package/src/core/permission-rules.ts +52 -4
- package/src/core/permission.ts +6 -2
- package/src/core/session-log.ts +10 -0
- package/src/i18n-core/locales/en-US.json +1 -1
- package/src/i18n-core/locales/zh-CN.json +1 -1
- package/src/index.tsx +22 -1
- package/src/mcp/client.ts +20 -1
- package/src/mcp/http-transport.ts +48 -3
- package/src/providers/anthropic.ts +47 -12
- package/src/providers/fetch-utils.ts +48 -3
- package/src/providers/openai-compat.ts +13 -10
- package/src/shared/package-info.ts +2 -2
- package/src/shared/sanitize.ts +14 -3
- package/src/shared/types.ts +12 -0
- package/src/skills/loader.ts +13 -2
- package/src/skills/sanitizer.ts +16 -8
- package/src/tools/artifact/artifact.ts +9 -9
- package/src/tools/file/read.ts +21 -0
- package/src/tools/network/web-fetch.ts +87 -22
- package/src/tools/scheduling/schedule-wakeup.ts +156 -32
- package/src/ui/app.tsx +46 -5
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, unlinkSync, readdirSync, existsSync } from 'node:fs'
|
|
2
|
+
import { createHash } from 'node:crypto'
|
|
3
|
+
import { join } from 'node:path'
|
|
1
4
|
import type { ToolDefinition } from '../../shared/index.ts'
|
|
5
|
+
import { atomicWriteFileSync } from '../../shared/atomic-write'
|
|
6
|
+
import { miphamHome } from '../../core/paths.ts'
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
9
|
* Active wakeup timers — in-memory, per-session.
|
|
@@ -6,6 +11,18 @@ import type { ToolDefinition } from '../../shared/index.ts'
|
|
|
6
11
|
*/
|
|
7
12
|
const activeTimers = new Map<string, ReturnType<typeof setTimeout>>()
|
|
8
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Where pending wakeups are written — one file per session.
|
|
16
|
+
*
|
|
17
|
+
* A `setTimeout` cannot fire into a process that is not running, and here that is
|
|
18
|
+
* not hypothetical: the daemon recycles an idle worker after 30 minutes
|
|
19
|
+
* (`daemon/worker-pool.ts`) while a wakeup may be up to 3600s out. So the *intent*
|
|
20
|
+
* is written here and the timer is rebuilt from it on the next start — a wakeup
|
|
21
|
+
* still in the future is restored exactly, and one that came due while we were gone
|
|
22
|
+
* is handed back to the model instead of vanishing (`resumeWakeups`).
|
|
23
|
+
*/
|
|
24
|
+
const WAKEUP_DIR = miphamHome('wakeups')
|
|
25
|
+
|
|
9
26
|
let wakeupHandler: ((sessionId: string, prompt: string, noop?: boolean) => void) | null = null
|
|
10
27
|
|
|
11
28
|
/** 引擎在启动时注入——timer 到期后回调,把 loop prompt 交回引擎 re-invoke。 */
|
|
@@ -15,6 +32,130 @@ export function registerWakeupHandler(
|
|
|
15
32
|
wakeupHandler = fn
|
|
16
33
|
}
|
|
17
34
|
|
|
35
|
+
/** One session's pending wakeup, as written to disk. */
|
|
36
|
+
export interface PersistedWakeup {
|
|
37
|
+
sessionId: string
|
|
38
|
+
prompt: string
|
|
39
|
+
noop: boolean
|
|
40
|
+
reason: string
|
|
41
|
+
/** When the timer was due (ISO). */
|
|
42
|
+
firesAt: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* File name for a session's slot. Hashed, not used literally: the session id is
|
|
47
|
+
* whatever `--resume <id>` was given, i.e. operator input, and a `../` in it must
|
|
48
|
+
* not walk out of this directory.
|
|
49
|
+
*/
|
|
50
|
+
function wakeupPath(sessionId: string): string {
|
|
51
|
+
const name = createHash('sha256').update(sessionId).digest('hex').slice(0, 32)
|
|
52
|
+
return join(WAKEUP_DIR, `${name}.json`)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function persistWakeup(entry: PersistedWakeup): void {
|
|
56
|
+
if (!existsSync(WAKEUP_DIR)) mkdirSync(WAKEUP_DIR, { recursive: true })
|
|
57
|
+
atomicWriteFileSync(wakeupPath(entry.sessionId), JSON.stringify(entry, null, 2), { mode: 0o644 })
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function clearPersistedWakeup(sessionId: string): void {
|
|
61
|
+
const path = wakeupPath(sessionId)
|
|
62
|
+
try {
|
|
63
|
+
if (existsSync(path)) unlinkSync(path)
|
|
64
|
+
} catch {
|
|
65
|
+
/* a file we cannot remove does not resurrect a timer we already cancelled */
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Every wakeup left on disk, across all sessions. Unreadable files are skipped. */
|
|
70
|
+
export function readPersistedWakeups(): PersistedWakeup[] {
|
|
71
|
+
let files: string[]
|
|
72
|
+
try {
|
|
73
|
+
files = readdirSync(WAKEUP_DIR)
|
|
74
|
+
} catch {
|
|
75
|
+
return []
|
|
76
|
+
}
|
|
77
|
+
const out: PersistedWakeup[] = []
|
|
78
|
+
for (const file of files) {
|
|
79
|
+
if (!file.endsWith('.json')) continue
|
|
80
|
+
try {
|
|
81
|
+
const parsed = JSON.parse(readFileSync(join(WAKEUP_DIR, file), 'utf-8')) as PersistedWakeup
|
|
82
|
+
if (parsed?.sessionId && parsed.firesAt) out.push(parsed)
|
|
83
|
+
} catch {
|
|
84
|
+
/* skip corrupt files */
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return out
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Arm the session's wakeup timer, replacing whatever timer it already had — one
|
|
92
|
+
* active wakeup per session, enforced here so every arming path (the tool, the
|
|
93
|
+
* restart path) obeys it rather than each remembering to cancel first.
|
|
94
|
+
*/
|
|
95
|
+
function armTimer(sessionId: string, prompt: string, noop: boolean, delayMs: number): void {
|
|
96
|
+
cancelSessionTimers(sessionId)
|
|
97
|
+
const timerKey = `${sessionId}:wakeup`
|
|
98
|
+
const timeoutId = setTimeout(() => {
|
|
99
|
+
activeTimers.delete(timerKey)
|
|
100
|
+
clearPersistedWakeup(sessionId)
|
|
101
|
+
wakeupHandler?.(sessionId, prompt, noop)
|
|
102
|
+
}, delayMs)
|
|
103
|
+
activeTimers.set(timerKey, timeoutId)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Drop this session's timers (in memory only — the store is the caller's business). */
|
|
107
|
+
function cancelSessionTimers(sessionId: string): number {
|
|
108
|
+
let count = 0
|
|
109
|
+
for (const [key, timer] of activeTimers) {
|
|
110
|
+
if (key.startsWith(sessionId + ':')) {
|
|
111
|
+
clearTimeout(timer)
|
|
112
|
+
activeTimers.delete(key)
|
|
113
|
+
count++
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return count
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A wakeup that came due while no process was running. */
|
|
120
|
+
export interface LostWakeup {
|
|
121
|
+
prompt: string
|
|
122
|
+
reason: string
|
|
123
|
+
firesAt: string
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Rebuild this session's pending wakeup after a restart. Returns the ones that
|
|
128
|
+
* came due while we were gone, and re-arms the rest.
|
|
129
|
+
*
|
|
130
|
+
* A due-while-down wakeup is **returned, not fired**. The delay the model picked
|
|
131
|
+
* measured from when it picked it: re-running that prompt blind hours later is not
|
|
132
|
+
* the loop iteration that was asked for. The caller hands it back to the model,
|
|
133
|
+
* which is the only party that can reschedule or stop. What must never happen is
|
|
134
|
+
* the silent drop — the model believing a wakeup is pending while nothing is armed
|
|
135
|
+
* and nothing is on disk.
|
|
136
|
+
*/
|
|
137
|
+
export function resumeWakeups(opts: { sessionId: string; now?: Date }): LostWakeup[] {
|
|
138
|
+
const now = (opts.now ?? new Date()).getTime()
|
|
139
|
+
const lost: LostWakeup[] = []
|
|
140
|
+
|
|
141
|
+
for (const entry of readPersistedWakeups()) {
|
|
142
|
+
if (entry.sessionId !== opts.sessionId) continue
|
|
143
|
+
const due = new Date(entry.firesAt).getTime()
|
|
144
|
+
if (!Number.isFinite(due)) {
|
|
145
|
+
clearPersistedWakeup(entry.sessionId)
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
if (due <= now) {
|
|
149
|
+
clearPersistedWakeup(entry.sessionId)
|
|
150
|
+
lost.push({ prompt: entry.prompt, reason: entry.reason, firesAt: entry.firesAt })
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
armTimer(entry.sessionId, entry.prompt, entry.noop, due - now)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return lost
|
|
157
|
+
}
|
|
158
|
+
|
|
18
159
|
export const scheduleWakeupTool: ToolDefinition = {
|
|
19
160
|
name: 'ScheduleWakeup',
|
|
20
161
|
description:
|
|
@@ -53,14 +194,7 @@ export const scheduleWakeupTool: ToolDefinition = {
|
|
|
53
194
|
|
|
54
195
|
// ── Stop — cancel all timers for this session ──
|
|
55
196
|
if (params.stop === true) {
|
|
56
|
-
|
|
57
|
-
for (const [key, timer] of activeTimers) {
|
|
58
|
-
if (key.startsWith(sessionId + ':')) {
|
|
59
|
-
clearTimeout(timer)
|
|
60
|
-
activeTimers.delete(key)
|
|
61
|
-
cancelled++
|
|
62
|
-
}
|
|
63
|
-
}
|
|
197
|
+
const cancelled = cancelAllSessionTimers(sessionId)
|
|
64
198
|
return {
|
|
65
199
|
success: true,
|
|
66
200
|
content: `Loop ended. ${cancelled} pending wakeup(s) cancelled.`,
|
|
@@ -89,21 +223,17 @@ export const scheduleWakeupTool: ToolDefinition = {
|
|
|
89
223
|
}
|
|
90
224
|
}
|
|
91
225
|
|
|
92
|
-
// Cancel any previous timer for this session (one active wakeup per session)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
wakeupHandler?.(sessionId, prompt, noop)
|
|
104
|
-
}, delaySeconds * 1000)
|
|
105
|
-
|
|
106
|
-
activeTimers.set(timerKey, timeoutId)
|
|
226
|
+
// Cancel any previous timer for this session (one active wakeup per session).
|
|
227
|
+
// Write the intent *before* arming: a wakeup that exists only in memory is the
|
|
228
|
+
// one that disappears when the process does.
|
|
229
|
+
persistWakeup({
|
|
230
|
+
sessionId,
|
|
231
|
+
prompt,
|
|
232
|
+
noop,
|
|
233
|
+
reason,
|
|
234
|
+
firesAt: new Date(Date.now() + delaySeconds * 1000).toISOString(),
|
|
235
|
+
})
|
|
236
|
+
armTimer(sessionId, prompt, noop, delaySeconds * 1000)
|
|
107
237
|
|
|
108
238
|
const mins = Math.floor(delaySeconds / 60)
|
|
109
239
|
const secs = delaySeconds % 60
|
|
@@ -118,15 +248,9 @@ export const scheduleWakeupTool: ToolDefinition = {
|
|
|
118
248
|
},
|
|
119
249
|
}
|
|
120
250
|
|
|
121
|
-
/** Cancel all timers for a session
|
|
251
|
+
/** Cancel all timers for a session and forget its pending wakeup. Session cleanup. */
|
|
122
252
|
export function cancelAllSessionTimers(sessionId: string): number {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (key.startsWith(sessionId + ':')) {
|
|
126
|
-
clearTimeout(timer)
|
|
127
|
-
activeTimers.delete(key)
|
|
128
|
-
count++
|
|
129
|
-
}
|
|
130
|
-
}
|
|
253
|
+
const count = cancelSessionTimers(sessionId)
|
|
254
|
+
clearPersistedWakeup(sessionId)
|
|
131
255
|
return count
|
|
132
256
|
}
|
package/src/ui/app.tsx
CHANGED
|
@@ -62,7 +62,8 @@ import { useI18n } from '../i18n-context'
|
|
|
62
62
|
import type { PermissionMode } from '../shared/index.ts'
|
|
63
63
|
import { sanitizeForDisplay } from '../shared/sanitize.ts'
|
|
64
64
|
import { recordLoopTurn, readAutoloopJournal } from '../commands/autoloop-journal.js'
|
|
65
|
-
import { cancelAllSessionTimers } from '../tools/scheduling/schedule-wakeup.js'
|
|
65
|
+
import { cancelAllSessionTimers, resumeWakeups } from '../tools/scheduling/schedule-wakeup.js'
|
|
66
|
+
import type { LostWakeup } from '../tools/scheduling/schedule-wakeup.js'
|
|
66
67
|
import { startCronPoller } from '../core/cron-poller'
|
|
67
68
|
|
|
68
69
|
interface AppProps {
|
|
@@ -224,6 +225,25 @@ function toolDisplayName(name: string): string {
|
|
|
224
225
|
return name
|
|
225
226
|
}
|
|
226
227
|
|
|
228
|
+
/**
|
|
229
|
+
* The turn a `/loop` gets when its wakeup came due while no process was running.
|
|
230
|
+
*
|
|
231
|
+
* Said plainly, and with the decision handed back: the loop was left armed by the
|
|
232
|
+
* model, so only the model can say whether to re-time it or end it. Silence here is
|
|
233
|
+
* what made the loss invisible — the model would keep treating a dead timer as
|
|
234
|
+
* pending work.
|
|
235
|
+
*/
|
|
236
|
+
function missedWakeupNotice(lost: LostWakeup): string {
|
|
237
|
+
return (
|
|
238
|
+
`⏰ Your scheduled wakeup never fired: it was due at ${lost.firesAt} ` +
|
|
239
|
+
`(${lost.reason}), and Mipham Code was not running then — nothing ran on your behalf. ` +
|
|
240
|
+
`The prompt you had queued was:\n"${lost.prompt}"\n\n` +
|
|
241
|
+
`The delay you chose measured from when you chose it, so this is not the iteration you ` +
|
|
242
|
+
`asked for. Decide now: reschedule with ScheduleWakeup (a fresh delaySeconds), or end ` +
|
|
243
|
+
`the loop with ScheduleWakeup {stop: true}.`
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
227
247
|
export function App({
|
|
228
248
|
engine,
|
|
229
249
|
config,
|
|
@@ -834,10 +854,11 @@ export function App({
|
|
|
834
854
|
setActiveTool(null)
|
|
835
855
|
setAgentTick((t) => t + 1)
|
|
836
856
|
}
|
|
837
|
-
//
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
857
|
+
// No checkpoint here. The automatic one is taken in `engine.process`, right
|
|
858
|
+
// after the prompt lands and before the reply does — so the newest
|
|
859
|
+
// checkpoint is the state `/rewind` is supposed to return to. Saving a
|
|
860
|
+
// second time at the end of the turn would put a snapshot *containing* that
|
|
861
|
+
// reply on top, and `/rewind` would restore what is already on screen.
|
|
841
862
|
// Final sync of background agents after the turn completes
|
|
842
863
|
syncBgAgents()
|
|
843
864
|
}
|
|
@@ -901,6 +922,26 @@ export function App({
|
|
|
901
922
|
return () => qe.setOnWakeupEnqueued(null)
|
|
902
923
|
}, [engine])
|
|
903
924
|
|
|
925
|
+
// ── /loop wakeup restore after a restart ──
|
|
926
|
+
// The timer is in memory, so an exit (or an idle daemon worker being recycled at
|
|
927
|
+
// 30 min, against a wakeup that may be 3600s out) used to drop a pending wakeup
|
|
928
|
+
// without a word — the model went on believing a loop iteration was coming.
|
|
929
|
+
// A wakeup still in the future is re-armed from disk here; one that came due
|
|
930
|
+
// while we were gone is handed to the model as the loop turn it is, so IT decides
|
|
931
|
+
// whether to re-time or end it. Declared *after* the subscription above: the
|
|
932
|
+
// enqueue below only mutates the queue, and it is that subscription that turns
|
|
933
|
+
// the mutation into the state bump the idle-drain effect reacts to.
|
|
934
|
+
useEffect(() => {
|
|
935
|
+
if (!('hasPendingWakeup' in engine)) return
|
|
936
|
+
const qe = engine
|
|
937
|
+
// `?? ''` and not a guard: the ToolContext this app builds writes
|
|
938
|
+
// `sessionId: sessionId || ''`, so that is the key a wakeup from here was
|
|
939
|
+
// stored under — looking it up under anything else would miss our own file.
|
|
940
|
+
for (const lost of resumeWakeups({ sessionId: sessionId ?? '' })) {
|
|
941
|
+
qe.enqueueWakeup(missedWakeupNotice(lost), false)
|
|
942
|
+
}
|
|
943
|
+
}, [engine, sessionId])
|
|
944
|
+
|
|
904
945
|
// When a wakeup is enqueued and the engine is idle, drain it now (the running turn's
|
|
905
946
|
// end-drain handles the busy case). `wakeupTick === 0` skips the mount-time run.
|
|
906
947
|
useEffect(() => {
|