peaks-loop-internal-runtime 0.0.38 → 0.0.40
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/dist/auto-compact-adapter.d.ts.map +1 -1
- package/dist/auto-compact-adapter.js +1 -1
- package/dist/dispatch.d.ts.map +1 -1
- package/dist/dispatch.js +25 -8
- package/dist/guards/json-object.d.ts +25 -0
- package/dist/guards/json-object.d.ts.map +1 -0
- package/dist/guards/json-object.js +26 -0
- package/dist/guards/resource-budget.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/process-supervisor.js +2 -2
- package/dist/prompt-builder.js +3 -3
- package/dist/status-protocol.d.ts +39 -2
- package/dist/status-protocol.d.ts.map +1 -1
- package/dist/status-protocol.js +60 -9
- package/dist/vendor/claude-adapter.d.ts +1 -0
- package/dist/vendor/claude-adapter.d.ts.map +1 -1
- package/dist/vendor/claude-adapter.js +4 -19
- package/dist/vendor/codex-adapter.d.ts +1 -0
- package/dist/vendor/codex-adapter.d.ts.map +1 -1
- package/dist/vendor/codex-adapter.js +3 -9
- package/dist/vendor/copilot-adapter.d.ts +1 -0
- package/dist/vendor/copilot-adapter.d.ts.map +1 -1
- package/dist/vendor/copilot-adapter.js +6 -10
- package/dist/vendor/detect-binary.d.ts.map +1 -1
- package/dist/vendor/progress-line.d.ts +31 -0
- package/dist/vendor/progress-line.d.ts.map +1 -0
- package/dist/vendor/progress-line.js +85 -0
- package/dist/vendor/registry.d.ts.map +1 -1
- package/dist/vendor/registry.js +9 -3
- package/package.json +2 -2
- package/src/auto-compact-adapter.ts +13 -5
- package/src/dispatch.ts +49 -21
- package/src/guards/json-object.ts +26 -0
- package/src/guards/resource-budget.ts +11 -4
- package/src/index.ts +2 -2
- package/src/lifecycle.ts +11 -5
- package/src/process-supervisor.ts +4 -4
- package/src/prompt-builder.ts +4 -4
- package/src/status-protocol.ts +113 -14
- package/src/types.ts +1 -1
- package/src/vendor/adapter.ts +1 -1
- package/src/vendor/claude-adapter.ts +5 -16
- package/src/vendor/codex-adapter.ts +4 -6
- package/src/vendor/copilot-adapter.ts +7 -7
- package/src/vendor/detect-binary.ts +8 -3
- package/src/vendor/progress-line.ts +86 -0
- package/src/vendor/registry.ts +11 -4
package/dist/vendor/registry.js
CHANGED
|
@@ -7,8 +7,14 @@ export class VendorAdapterRegistry {
|
|
|
7
7
|
for (const a of initial)
|
|
8
8
|
this.map.set(a.id, a);
|
|
9
9
|
}
|
|
10
|
-
register(a) {
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
register(a) {
|
|
11
|
+
this.map.set(a.id, a);
|
|
12
|
+
}
|
|
13
|
+
get(id) {
|
|
14
|
+
return this.map.get(id);
|
|
15
|
+
}
|
|
16
|
+
list() {
|
|
17
|
+
return [...this.map.values()];
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
export const defaultRegistry = () => new VendorAdapterRegistry([new ClaudeAdapter(), new CodexAdapter(), new CopilotAdapter()]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "peaks-loop-internal-runtime",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.40",
|
|
4
4
|
"description": "Detached sub-agent runtime: ProcessSupervisor + LifecycleOwner + VendorAdapter (claude/codex/copilot) + G8 infinite-context auto-compact. Slice 2026-08-10.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"peaks-loop-shared": "0.0.
|
|
19
|
+
"peaks-loop-shared": "0.0.89"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"typescript": "^5.4.0",
|
|
@@ -10,10 +10,17 @@ import type { AutoCompactEvent } from './status-protocol.js';
|
|
|
10
10
|
* tell that it is fabricated. When the caller genuinely knows the window it is
|
|
11
11
|
* still carried verbatim (E5, rid 2026-09-13-defects-e).
|
|
12
12
|
*/
|
|
13
|
-
export interface MarkerOpts {
|
|
13
|
+
export interface MarkerOpts {
|
|
14
|
+
rid: string;
|
|
15
|
+
sid: string;
|
|
16
|
+
vendorWindow?: number;
|
|
17
|
+
}
|
|
14
18
|
export interface ScratchPayload {
|
|
15
|
-
seq: number;
|
|
16
|
-
|
|
19
|
+
seq: number;
|
|
20
|
+
at: number;
|
|
21
|
+
summary: string;
|
|
22
|
+
decisionsKept?: string[];
|
|
23
|
+
discardedOptions?: string[];
|
|
17
24
|
}
|
|
18
25
|
|
|
19
26
|
/**
|
|
@@ -47,7 +54,8 @@ export interface ScratchPayload {
|
|
|
47
54
|
*/
|
|
48
55
|
export class AutoCompactAdapter {
|
|
49
56
|
marker(opts: MarkerOpts): string {
|
|
50
|
-
const windowAttr =
|
|
57
|
+
const windowAttr =
|
|
58
|
+
opts.vendorWindow === undefined ? '' : ` vendor-window="${opts.vendorWindow}"`;
|
|
51
59
|
return [
|
|
52
60
|
`<peaks-auto-compact threshold="0.85|0.95"${windowAttr}>`,
|
|
53
61
|
`上下文续命协议(如实版 —— 只写你确实能做的事):`,
|
|
@@ -65,7 +73,7 @@ export class AutoCompactAdapter {
|
|
|
65
73
|
`- 这些文件由你自己维护:不要等 peaks 主进程来催,它不会替你写。`,
|
|
66
74
|
`- token 费用:peaks-loop 不设费用上限、也不因费用中断你(没有费用闸门)。`,
|
|
67
75
|
` 这不对应任何 flag —— 它是 peaks-loop 没有做的事,不是一条你能引用的授权。`,
|
|
68
|
-
`</peaks-auto-compact
|
|
76
|
+
`</peaks-auto-compact>`
|
|
69
77
|
].join('\n');
|
|
70
78
|
}
|
|
71
79
|
|
package/src/dispatch.ts
CHANGED
|
@@ -8,15 +8,18 @@ import { ClaudeAdapter } from './vendor/claude-adapter.js';
|
|
|
8
8
|
import { CodexAdapter } from './vendor/codex-adapter.js';
|
|
9
9
|
import { CopilotAdapter } from './vendor/copilot-adapter.js';
|
|
10
10
|
import { PromptBuilder } from './prompt-builder.js';
|
|
11
|
-
import { StatusProtocol } from './status-protocol.js';
|
|
12
11
|
import { AutoCompactAdapter } from './auto-compact-adapter.js';
|
|
13
|
-
import { ResourceBudgetGuard } from './guards/resource-budget.js';
|
|
14
12
|
|
|
15
13
|
export interface DispatchInput {
|
|
16
|
-
sid: string;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
sid: string;
|
|
15
|
+
rid: string;
|
|
16
|
+
role: 'rd' | 'qa' | 'ui' | 'txt' | 'general-purpose';
|
|
17
|
+
vendor: 'claude' | 'codex' | 'copilot';
|
|
18
|
+
userTask: string;
|
|
19
|
+
files: string[];
|
|
20
|
+
refs: string[];
|
|
21
|
+
runtimeDir: string;
|
|
22
|
+
subAgentsDir: string;
|
|
20
23
|
verbatimBlocks?: string[];
|
|
21
24
|
}
|
|
22
25
|
export interface DispatchResult {
|
|
@@ -33,7 +36,11 @@ export interface DispatchResult {
|
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
export async function dispatchDetached(i: DispatchInput): Promise<DispatchResult> {
|
|
36
|
-
const registry = new VendorAdapterRegistry([
|
|
39
|
+
const registry = new VendorAdapterRegistry([
|
|
40
|
+
new ClaudeAdapter(),
|
|
41
|
+
new CodexAdapter(),
|
|
42
|
+
new CopilotAdapter()
|
|
43
|
+
]);
|
|
37
44
|
const adapter = registry.get(i.vendor);
|
|
38
45
|
if (!adapter) throw new Error(`vendor adapter not registered: ${i.vendor}`);
|
|
39
46
|
|
|
@@ -48,9 +55,13 @@ export async function dispatchDetached(i: DispatchInput): Promise<DispatchResult
|
|
|
48
55
|
// than invented.
|
|
49
56
|
const marker = ac.marker({ rid: i.rid, sid: i.sid });
|
|
50
57
|
const prompt = pb.assemble({
|
|
51
|
-
rid: i.rid,
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
rid: i.rid,
|
|
59
|
+
role: i.role,
|
|
60
|
+
vendor: i.vendor,
|
|
61
|
+
files: i.files,
|
|
62
|
+
refs: i.refs,
|
|
63
|
+
userTask: i.userTask,
|
|
64
|
+
verbatimBlocks: [marker, ...(i.verbatimBlocks ?? [])]
|
|
54
65
|
});
|
|
55
66
|
|
|
56
67
|
const args = adapter.headlessArgs(prompt, { autoCompactMarker: marker });
|
|
@@ -72,17 +83,34 @@ export async function dispatchDetached(i: DispatchInput): Promise<DispatchResult
|
|
|
72
83
|
const spawnError = (await handle.settled) ?? null;
|
|
73
84
|
lo.register(handle.pid, i.rid, i.sid);
|
|
74
85
|
|
|
86
|
+
const recPath = writeDispatchRecord(i, spawnError);
|
|
87
|
+
|
|
88
|
+
return { pid: handle.pid, dispatchRecordPath: recPath, child: handle.child, spawnError };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function writeDispatchRecord(i: DispatchInput, spawnError: NodeJS.ErrnoException | null): string {
|
|
75
92
|
// Write dispatch record (placeholder — final shape per Task 8 schema)
|
|
76
93
|
const recPath = join(i.subAgentsDir, `dispatch-${i.rid}-${Date.now()}.json`);
|
|
77
94
|
mkdirSync(i.subAgentsDir, { recursive: true });
|
|
78
|
-
writeFileSync(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
writeFileSync(
|
|
96
|
+
recPath,
|
|
97
|
+
JSON.stringify(
|
|
98
|
+
{
|
|
99
|
+
rid: i.rid,
|
|
100
|
+
mode: 'detached',
|
|
101
|
+
vendor: i.vendor,
|
|
102
|
+
// The record must not claim a child is running when the launch failed —
|
|
103
|
+
// that is the on-disk form of "looks green, checked nothing".
|
|
104
|
+
status: spawnError ? 'failed' : 'running',
|
|
105
|
+
...(spawnError
|
|
106
|
+
? { spawnError: { code: spawnError.code, message: spawnError.message } }
|
|
107
|
+
: {}),
|
|
108
|
+
heartbeats: [],
|
|
109
|
+
at: Date.now()
|
|
110
|
+
},
|
|
111
|
+
null,
|
|
112
|
+
2
|
|
113
|
+
)
|
|
114
|
+
);
|
|
115
|
+
return recPath;
|
|
116
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "This unknown value is a plain JSON object" — the one narrowing this package
|
|
3
|
+
* uses to open a value that came out of `JSON.parse`.
|
|
4
|
+
*
|
|
5
|
+
* WHY IT LIVES HERE RATHER THAN IN EACH READER (batch B2). Two readers in this
|
|
6
|
+
* package need to open a parsed value before they can check its fields:
|
|
7
|
+
* `src/status-protocol.ts` (a status record) and `src/vendor/progress-line.ts`
|
|
8
|
+
* (a progress line). The alternative to this function is not "no function" — it
|
|
9
|
+
* is `const rec: Record<string, unknown> = JSON.parse(raw)`, which is a CAST:
|
|
10
|
+
* it asserts the shape instead of checking it, and because `JSON.parse` returns
|
|
11
|
+
* `any` the assertion is invisible to the TS lint ratchet. So the check is
|
|
12
|
+
* written out once, at the boundary, and both readers call it.
|
|
13
|
+
*
|
|
14
|
+
* The three clauses are each load-bearing:
|
|
15
|
+
* - `typeof value === 'object'` rejects strings, numbers, booleans, bigints,
|
|
16
|
+
* symbols, undefined and functions in one go;
|
|
17
|
+
* - `value !== null` — `typeof null` is the famous `'object'`;
|
|
18
|
+
* - `!Array.isArray(value)` — an array IS a JSON value, and this predicate is
|
|
19
|
+
* a named narrowing (NOT the repo's non-narrowing `src/shared/array-guards.ts`
|
|
20
|
+
* `isArray`, which this package cannot import and whose job is the opposite
|
|
21
|
+
* one). An array has no named fields, so treating one as an object would
|
|
22
|
+
* hand every field read `undefined`.
|
|
23
|
+
*/
|
|
24
|
+
export function isJsonObject(value: unknown): value is Record<string, unknown> {
|
|
25
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
26
|
+
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
export interface Sample {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export interface Sample {
|
|
2
|
+
rssMb: number;
|
|
3
|
+
cpuPct: number;
|
|
4
|
+
}
|
|
5
|
+
export interface EnforceInput {
|
|
6
|
+
active: number;
|
|
7
|
+
}
|
|
8
|
+
export interface EnforceOpts {
|
|
9
|
+
maxConcurrent: number;
|
|
10
|
+
}
|
|
4
11
|
|
|
5
12
|
export class ResourceBudgetGuard {
|
|
6
13
|
constructor(private readonly cfg: { maxRssMb: number; maxCpuPct: number }) {}
|
|
@@ -17,4 +24,4 @@ export class ResourceBudgetGuard {
|
|
|
17
24
|
if (input.active > opts.maxConcurrent) return { throttle: true };
|
|
18
25
|
return { throttle: false };
|
|
19
26
|
}
|
|
20
|
-
}
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// API contract: RUNTIME_VERSION tracks peaks-loop root version (4.0.x).
|
|
2
2
|
// This is what consumers (sub-agents dispatched by peaks-code) check at
|
|
3
3
|
// runtime to verify protocol compatibility.
|
|
4
|
-
export const RUNTIME_VERSION = '4.0
|
|
4
|
+
export const RUNTIME_VERSION = '4.1.0';
|
|
5
5
|
|
|
6
6
|
// npm version: independent 0.0.x SemVer. Runtime is its own package
|
|
7
7
|
// (peaks-loop-internal-runtime@NPM_VERSION) on the registry; bumps
|
|
@@ -21,4 +21,4 @@ export type { HeartbeatEntry, AutoCompactEvent } from './status-protocol.js';
|
|
|
21
21
|
export { AutoCompactAdapter } from './auto-compact-adapter.js';
|
|
22
22
|
export { ResourceBudgetGuard } from './guards/resource-budget.js';
|
|
23
23
|
export { dispatchDetached } from './dispatch.js';
|
|
24
|
-
export type { DispatchInput, DispatchResult } from './dispatch.js';
|
|
24
|
+
export type { DispatchInput, DispatchResult } from './dispatch.js';
|
package/src/lifecycle.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { existsSync,
|
|
1
|
+
import { existsSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
|
|
4
|
-
interface ActiveRecord {
|
|
4
|
+
interface ActiveRecord {
|
|
5
|
+
pid: number;
|
|
6
|
+
rid: string;
|
|
7
|
+
ownerSession: string;
|
|
8
|
+
}
|
|
5
9
|
|
|
6
10
|
export class LifecycleOwner {
|
|
7
11
|
private active = new Map<string, ActiveRecord>();
|
|
@@ -19,8 +23,10 @@ export class LifecycleOwner {
|
|
|
19
23
|
writeFileSync(join(dir, 'exit.json'), JSON.stringify(exit));
|
|
20
24
|
|
|
21
25
|
// Archive
|
|
22
|
-
if (existsSync(join(dir, 'log.txt')))
|
|
23
|
-
|
|
26
|
+
if (existsSync(join(dir, 'log.txt')))
|
|
27
|
+
renameSync(join(dir, 'log.txt'), join(dir, 'log-archive.txt'));
|
|
28
|
+
if (existsSync(join(dir, 'status.json')))
|
|
29
|
+
renameSync(join(dir, 'status.json'), join(dir, 'status-final.json'));
|
|
24
30
|
|
|
25
31
|
// Delete active markers
|
|
26
32
|
for (const f of ['pid', 'owner-session']) {
|
|
@@ -37,4 +43,4 @@ export class LifecycleOwner {
|
|
|
37
43
|
}
|
|
38
44
|
return orphans; // user decides via `peaks sub-agent cleanup --orphan`
|
|
39
45
|
}
|
|
40
|
-
}
|
|
46
|
+
}
|
|
@@ -45,7 +45,7 @@ export class ProcessSupervisor {
|
|
|
45
45
|
// pipes for capture + lifecycle control.
|
|
46
46
|
const spawnOpts: SpawnOptions = {
|
|
47
47
|
detached: false,
|
|
48
|
-
stdio: opts.stdio ?? 'pipe'
|
|
48
|
+
stdio: opts.stdio ?? 'pipe'
|
|
49
49
|
};
|
|
50
50
|
if (isWin) {
|
|
51
51
|
// Suppress the popup console window on Windows without changing
|
|
@@ -70,7 +70,7 @@ export class ProcessSupervisor {
|
|
|
70
70
|
// construction, whatever caller writes it. Capturing it here turns the
|
|
71
71
|
// failure into a typed value on `settled` and removes the crash.
|
|
72
72
|
const settled = new Promise<NodeJS.ErrnoException | null>((resolve) => {
|
|
73
|
-
child.on('error', (err: Error) => resolve(err
|
|
73
|
+
child.on('error', (err: Error) => resolve(err));
|
|
74
74
|
child.on('spawn', () => resolve(null));
|
|
75
75
|
});
|
|
76
76
|
|
|
@@ -96,7 +96,7 @@ export class ProcessSupervisor {
|
|
|
96
96
|
pid: child.pid ?? -1,
|
|
97
97
|
child,
|
|
98
98
|
kill: (signal: NodeJS.Signals = 'SIGTERM') => child.kill(signal),
|
|
99
|
-
settled
|
|
99
|
+
settled
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
|
-
}
|
|
102
|
+
}
|
package/src/prompt-builder.ts
CHANGED
|
@@ -22,15 +22,15 @@ export class PromptBuilder {
|
|
|
22
22
|
i.userTask,
|
|
23
23
|
``,
|
|
24
24
|
`## Files (read-only)`,
|
|
25
|
-
...i.files.map(f => `- ${f}`),
|
|
25
|
+
...i.files.map((f) => `- ${f}`),
|
|
26
26
|
``,
|
|
27
27
|
`## References`,
|
|
28
|
-
...i.refs.map(r => `- ${r}`),
|
|
28
|
+
...i.refs.map((r) => `- ${r}`),
|
|
29
29
|
``,
|
|
30
|
-
...(i.verbatimBlocks ?? [])
|
|
30
|
+
...(i.verbatimBlocks ?? [])
|
|
31
31
|
];
|
|
32
32
|
const out = parts.join('\n');
|
|
33
33
|
if (out.includes(FORBIDDEN)) throw new Error('forbidden marker leak');
|
|
34
34
|
return out;
|
|
35
35
|
}
|
|
36
|
-
}
|
|
36
|
+
}
|
package/src/status-protocol.ts
CHANGED
|
@@ -1,23 +1,122 @@
|
|
|
1
1
|
import type { ChildStatus } from './types.js';
|
|
2
|
+
import { isJsonObject } from './guards/json-object.js';
|
|
2
3
|
|
|
3
|
-
export interface HeartbeatEntry {
|
|
4
|
-
|
|
4
|
+
export interface HeartbeatEntry {
|
|
5
|
+
progress: number;
|
|
6
|
+
note: string;
|
|
7
|
+
ts: number;
|
|
8
|
+
}
|
|
9
|
+
export interface AutoCompactEvent {
|
|
10
|
+
at: number;
|
|
11
|
+
threshold: '0.85' | '0.95';
|
|
12
|
+
tokensBefore: number;
|
|
13
|
+
tokensAfter: number;
|
|
14
|
+
scratchFile?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A status record as it comes off disk: an OPEN JSON object.
|
|
19
|
+
*
|
|
20
|
+
* WHY NOT `heartbeats?: HeartbeatEntry[]` — the honest reason, because the two
|
|
21
|
+
* look equivalent and are not. The record arrives from `JSON.parse`, which
|
|
22
|
+
* hands back `any`; nothing between the disk and this class has checked that
|
|
23
|
+
* `heartbeats` is an array, or that its elements are heartbeat entries. Writing
|
|
24
|
+
* `heartbeats?: HeartbeatEntry[]` on the interface would therefore be a CLAIM
|
|
25
|
+
* about a shape nobody verified — the same offence as `as`, only harder to
|
|
26
|
+
* see, because it reads as a type rather than a cast.
|
|
27
|
+
*
|
|
28
|
+
* So the record is declared as what it really is (an object with unknown
|
|
29
|
+
* values) and the two arrays this class appends to are READ through
|
|
30
|
+
* `readHeartbeats` / `readCompactEvents`, which check and drop. That is why
|
|
31
|
+
* those readers return the concrete types below rather than the field being
|
|
32
|
+
* declared pre-typed.
|
|
33
|
+
*/
|
|
34
|
+
export interface StatusRecord {
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What `merge` returns: the record it was given, plus the fields it wrote. */
|
|
39
|
+
export type MergedStatusRecord = StatusRecord & {
|
|
40
|
+
heartbeats: HeartbeatEntry[];
|
|
41
|
+
/** Written only when the 100-entry cap actually fired (see `merge`). */
|
|
42
|
+
heartbeatsTruncated?: boolean;
|
|
43
|
+
status: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** What `appendCompactEvent` returns: the record, plus the event it appended. */
|
|
47
|
+
export type CompactedStatusRecord = StatusRecord & {
|
|
48
|
+
autoCompactEvents: AutoCompactEvent[];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
function isHeartbeatEntry(value: unknown): value is HeartbeatEntry {
|
|
52
|
+
return (
|
|
53
|
+
isJsonObject(value) &&
|
|
54
|
+
typeof value['progress'] === 'number' &&
|
|
55
|
+
typeof value['note'] === 'string' &&
|
|
56
|
+
typeof value['ts'] === 'number'
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isCompactEvent(value: unknown): value is AutoCompactEvent {
|
|
61
|
+
return (
|
|
62
|
+
isJsonObject(value) &&
|
|
63
|
+
typeof value['at'] === 'number' &&
|
|
64
|
+
(value['threshold'] === '0.85' || value['threshold'] === '0.95') &&
|
|
65
|
+
typeof value['tokensBefore'] === 'number' &&
|
|
66
|
+
typeof value['tokensAfter'] === 'number' &&
|
|
67
|
+
(value['scratchFile'] === undefined || typeof value['scratchFile'] === 'string')
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The heartbeat entries carried by `rec`, in order, with anything that is not a
|
|
73
|
+
* heartbeat entry dropped. `rec.heartbeats` being absent, a non-array, or an
|
|
74
|
+
* array of junk all read as "no heartbeats yet" — the same outcome the old
|
|
75
|
+
* `rec.heartbeats ?? []` produced for the absent case, extended to the cases it
|
|
76
|
+
* silently spread into a malformed array.
|
|
77
|
+
*/
|
|
78
|
+
function readHeartbeats(rec: StatusRecord): HeartbeatEntry[] {
|
|
79
|
+
const raw = rec['heartbeats'];
|
|
80
|
+
if (!Array.isArray(raw)) return [];
|
|
81
|
+
const out: HeartbeatEntry[] = [];
|
|
82
|
+
for (const item of raw) {
|
|
83
|
+
if (isHeartbeatEntry(item)) out.push(item);
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The compact events carried by `rec`. Same contract as `readHeartbeats`. */
|
|
89
|
+
function readCompactEvents(rec: StatusRecord): AutoCompactEvent[] {
|
|
90
|
+
const raw = rec['autoCompactEvents'];
|
|
91
|
+
if (!Array.isArray(raw)) return [];
|
|
92
|
+
const out: AutoCompactEvent[] = [];
|
|
93
|
+
for (const item of raw) {
|
|
94
|
+
if (isCompactEvent(item)) out.push(item);
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
5
98
|
|
|
6
99
|
export class StatusProtocol {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return
|
|
100
|
+
/**
|
|
101
|
+
* Append one heartbeat and refresh `status`. Every other key the record
|
|
102
|
+
* carried is preserved; `heartbeats` is capped at the last 100 and
|
|
103
|
+
* `heartbeatsTruncated` is written only when that cap fired.
|
|
104
|
+
*/
|
|
105
|
+
merge(rec: StatusRecord, s: ChildStatus): MergedStatusRecord {
|
|
106
|
+
const appended = [...readHeartbeats(rec), { progress: s.progress, note: s.note, ts: s.ts }];
|
|
107
|
+
const truncated = appended.length > 100;
|
|
108
|
+
return {
|
|
109
|
+
...rec,
|
|
110
|
+
heartbeats: truncated ? appended.slice(-100) : appended,
|
|
111
|
+
...(truncated ? { heartbeatsTruncated: true } : {}),
|
|
112
|
+
status: s.state === 'running' ? 'running' : s.state
|
|
113
|
+
};
|
|
16
114
|
}
|
|
17
115
|
isStale(lastBeatAt: number, thresholdSec = 300): boolean {
|
|
18
116
|
return Date.now() - lastBeatAt > thresholdSec * 1000;
|
|
19
117
|
}
|
|
20
|
-
|
|
21
|
-
|
|
118
|
+
/** Append one compact event, preserving every other key on the record. */
|
|
119
|
+
appendCompactEvent(rec: StatusRecord, ev: AutoCompactEvent): CompactedStatusRecord {
|
|
120
|
+
return { ...rec, autoCompactEvents: [...readCompactEvents(rec), ev] };
|
|
22
121
|
}
|
|
23
|
-
}
|
|
122
|
+
}
|
package/src/types.ts
CHANGED
package/src/vendor/adapter.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { VendorAdapter } from './adapter.js';
|
|
2
2
|
import type { ChildStatus } from '../types.js';
|
|
3
3
|
import { detectBinaryInstalled } from './detect-binary.js';
|
|
4
|
+
import { parseProgressLine } from './progress-line.js';
|
|
4
5
|
|
|
5
6
|
export class ClaudeAdapter implements VendorAdapter {
|
|
6
7
|
readonly id = 'claude' as const;
|
|
@@ -8,28 +9,16 @@ export class ClaudeAdapter implements VendorAdapter {
|
|
|
8
9
|
readonly maxPromptBytes = 8 * 1024;
|
|
9
10
|
|
|
10
11
|
headlessArgs(prompt: string, opts?: { autoCompactMarker?: string }): string[] {
|
|
11
|
-
const injected = opts?.autoCompactMarker
|
|
12
|
-
? `${opts.autoCompactMarker}\n\n${prompt}`
|
|
13
|
-
: prompt;
|
|
12
|
+
const injected = opts?.autoCompactMarker ? `${opts.autoCompactMarker}\n\n${prompt}` : prompt;
|
|
14
13
|
return ['-p', injected, '--output-format', 'json', '--include-partial-messages'];
|
|
15
14
|
}
|
|
16
15
|
|
|
16
|
+
/** The line format is shared with the other two vendors — see `progress-line.ts`. */
|
|
17
17
|
parseStatusLine(stdout: string): ChildStatus | null {
|
|
18
|
-
|
|
19
|
-
const obj = JSON.parse(stdout);
|
|
20
|
-
if (typeof obj.progress !== 'number') return null;
|
|
21
|
-
return {
|
|
22
|
-
rid: String(obj.rid ?? ''),
|
|
23
|
-
vendor: 'claude',
|
|
24
|
-
progress: obj.progress,
|
|
25
|
-
state: obj.state,
|
|
26
|
-
note: String(obj.note ?? ''),
|
|
27
|
-
ts: Number(obj.ts ?? Date.now()),
|
|
28
|
-
};
|
|
29
|
-
} catch { return null; }
|
|
18
|
+
return parseProgressLine(stdout, 'claude');
|
|
30
19
|
}
|
|
31
20
|
|
|
32
21
|
async detectInstalled(): Promise<boolean> {
|
|
33
22
|
return detectBinaryInstalled(this.binary);
|
|
34
23
|
}
|
|
35
|
-
}
|
|
24
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { VendorAdapter } from './adapter.js';
|
|
2
2
|
import type { ChildStatus } from '../types.js';
|
|
3
3
|
import { detectBinaryInstalled } from './detect-binary.js';
|
|
4
|
+
import { parseProgressLine } from './progress-line.js';
|
|
4
5
|
|
|
5
6
|
export class CodexAdapter implements VendorAdapter {
|
|
6
7
|
readonly id = 'codex' as const;
|
|
@@ -9,14 +10,11 @@ export class CodexAdapter implements VendorAdapter {
|
|
|
9
10
|
headlessArgs(prompt: string): string[] {
|
|
10
11
|
return ['exec', '--json', prompt];
|
|
11
12
|
}
|
|
13
|
+
/** The line format is shared with the other two vendors — see `progress-line.ts`. */
|
|
12
14
|
parseStatusLine(stdout: string): ChildStatus | null {
|
|
13
|
-
|
|
14
|
-
const o = JSON.parse(stdout);
|
|
15
|
-
if (typeof o.progress !== 'number') return null;
|
|
16
|
-
return { rid: String(o.rid ?? ''), vendor: 'codex', progress: o.progress, state: o.state, note: String(o.note ?? ''), ts: Number(o.ts ?? Date.now()) };
|
|
17
|
-
} catch { return null; }
|
|
15
|
+
return parseProgressLine(stdout, 'codex');
|
|
18
16
|
}
|
|
19
17
|
async detectInstalled(): Promise<boolean> {
|
|
20
18
|
return detectBinaryInstalled(this.binary);
|
|
21
19
|
}
|
|
22
|
-
}
|
|
20
|
+
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import type { VendorAdapter } from './adapter.js';
|
|
2
2
|
import type { ChildStatus } from '../types.js';
|
|
3
3
|
import { detectBinaryInstalled } from './detect-binary.js';
|
|
4
|
+
import { parseProgressLine } from './progress-line.js';
|
|
4
5
|
|
|
5
6
|
export class CopilotAdapter implements VendorAdapter {
|
|
6
7
|
readonly id = 'copilot' as const;
|
|
7
8
|
readonly binary = 'copilot';
|
|
8
9
|
readonly maxPromptBytes = 6 * 1024;
|
|
9
|
-
headlessArgs(prompt: string): string[] {
|
|
10
|
+
headlessArgs(prompt: string): string[] {
|
|
11
|
+
return ['-p', prompt, '--output-format', 'json'];
|
|
12
|
+
}
|
|
13
|
+
/** The line format is shared with the other two vendors — see `progress-line.ts`. */
|
|
10
14
|
parseStatusLine(stdout: string): ChildStatus | null {
|
|
11
|
-
|
|
12
|
-
const o = JSON.parse(stdout);
|
|
13
|
-
if (typeof o.progress !== 'number') return null;
|
|
14
|
-
return { rid: String(o.rid ?? ''), vendor: 'copilot', progress: o.progress, state: o.state, note: String(o.note ?? ''), ts: Number(o.ts ?? Date.now()) };
|
|
15
|
-
} catch { return null; }
|
|
15
|
+
return parseProgressLine(stdout, 'copilot');
|
|
16
16
|
}
|
|
17
17
|
async detectInstalled(): Promise<boolean> {
|
|
18
18
|
return detectBinaryInstalled(this.binary);
|
|
19
19
|
}
|
|
20
|
-
}
|
|
20
|
+
}
|
|
@@ -7,8 +7,13 @@ export function detectBinaryInstalled(binary: string): Promise<boolean> {
|
|
|
7
7
|
const locator = process.platform === 'win32' ? 'where.exe' : 'which';
|
|
8
8
|
|
|
9
9
|
return new Promise((resolve) => {
|
|
10
|
-
execFile(
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
execFile(
|
|
11
|
+
locator,
|
|
12
|
+
[binary],
|
|
13
|
+
{ timeout: DETECT_TIMEOUT_MS, windowsHide: true },
|
|
14
|
+
(error, stdout) => {
|
|
15
|
+
resolve(error === null && stdout.trim().length > 0);
|
|
16
|
+
}
|
|
17
|
+
);
|
|
13
18
|
});
|
|
14
19
|
}
|