jg-agent-hq 13.2.0 → 13.3.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.
- package/CHANGELOG.md +11 -0
- package/README.md +4 -0
- package/api/README.md +32 -14
- package/dist/actions/actions.d.ts +2 -1
- package/dist/actions/actions.js +2 -1
- package/dist/actions/events/events.js +52 -3
- package/dist/actions/live/live.js +1 -0
- package/dist/actions/types.d.ts +2 -0
- package/dist/contracts/actions/types.d.ts +11 -11
- package/dist/contracts/async-inputs/index.d.ts +2 -0
- package/dist/contracts/async-inputs/index.js +2 -0
- package/dist/contracts/async-inputs/types.d.ts +23 -0
- package/dist/contracts/async-inputs/types.js +23 -0
- package/dist/contracts/browser/types.d.ts +25 -10
- package/dist/contracts/evidence/types.d.ts +32 -2
- package/dist/contracts/evidence/types.js +2 -0
- package/dist/contracts/index.d.ts +2 -0
- package/dist/contracts/index.js +2 -0
- package/dist/contracts/providers/types.d.ts +27 -27
- package/dist/contracts/skills/types.d.ts +1 -1
- package/dist/contracts/types.d.ts +231 -66
- package/dist/contracts/types.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +0 -1
- package/dist/providers/claude/adapter.js +10 -1
- package/dist/providers/claude/control/control.d.ts +4 -2
- package/dist/providers/claude/control/control.js +35 -1
- package/dist/providers/claude/control/stream.d.ts +1 -1
- package/dist/providers/claude/control/stream.js +3 -2
- package/dist/providers/claude/control/types.d.ts +4 -1
- package/dist/providers/codex/events.js +2 -1
- package/dist/providers/codex/transformers.js +20 -1
- package/dist/providers/types.d.ts +2 -2
- package/dist/runtime/async-inputs/async-inputs.d.ts +16 -0
- package/dist/runtime/async-inputs/async-inputs.js +73 -0
- package/dist/runtime/async-inputs/index.d.ts +2 -0
- package/dist/runtime/async-inputs/index.js +2 -0
- package/dist/runtime/async-inputs/types.d.ts +18 -0
- package/dist/runtime/async-inputs/types.js +1 -0
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.js +0 -1
- package/dist/runtime/runtime.js +9 -2
- package/dist/runtime/types.d.ts +9 -0
- package/dist/sessions/evidence/origins.d.ts +5 -1
- package/dist/sessions/evidence/origins.js +13 -3
- package/dist/sessions/evidence/utilities.js +1 -0
- package/dist/sessions/sessions.d.ts +1 -1
- package/dist/sessions/sessions.js +5 -2
- package/dist/store/control/outcomes/outcomes.model.d.ts +9 -0
- package/dist/store/control/outcomes/outcomes.model.js +27 -0
- package/docs/recovery.md +4 -0
- package/package.json +1 -1
package/dist/contracts/types.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { AsyncInputSchema } from './async-inputs/index.js';
|
|
2
3
|
import { EvidenceRequestSchema, EvidencePageSchema, HistoryKindSchema, HistoryProvenanceSchema, } from './evidence/index.js';
|
|
3
4
|
import { ProviderCapabilitiesSchema, ProviderIdSchema, ProviderStatusSchema, } from './providers/index.js';
|
|
4
5
|
import { DEFAULT_PAGE_SIZE, MAX_ITEM_IMAGE_BYTES, MAX_ITEM_IMAGES, MAX_ITEM_TEXT_LENGTH, MAX_PAGE_SIZE, MAX_WAIT_MS, MAX_WORKSPACE_ROOTS, PUBLIC_ERROR_MESSAGES, } from './constants.js';
|
|
@@ -221,6 +222,7 @@ export const HistoryItemSchema = z.object({
|
|
|
221
222
|
provenance: HistoryProvenanceSchema,
|
|
222
223
|
text: z.string(),
|
|
223
224
|
isTruncated: z.boolean(),
|
|
225
|
+
asyncInput: AsyncInputSchema.optional(),
|
|
224
226
|
callId: z.string().min(1).max(4096).nullable(),
|
|
225
227
|
outcome: z.enum(['succeeded', 'failed']).nullable(),
|
|
226
228
|
files: z.array(HistoryFileSchema).max(MAX_PAGE_SIZE),
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export type { IAsyncInputSource, IAsyncInputEvent, IAsyncInputListener } from './runtime/index.js';
|
|
1
2
|
export { createAgentHq } from './runtime/index.js';
|
|
2
3
|
export type { IAgentHq, IAgentHqOptions, IPreviousRuntimeCheck, IPreviousRuntimeOwner, } from './runtime/index.js';
|
|
3
4
|
export { requestAgentHq } from './ipc/index.js';
|
package/dist/index.js
CHANGED
|
@@ -85,7 +85,16 @@ export const createClaudeProviderFactory = (store, additionalDirectories) => ({
|
|
|
85
85
|
});
|
|
86
86
|
},
|
|
87
87
|
listThreads: (workspaceRoot, page, call) => reader.listThreads(workspaceRoot, page, call.signal),
|
|
88
|
-
readHistory
|
|
88
|
+
async readHistory(command) {
|
|
89
|
+
const response = await reader.readHistory(command);
|
|
90
|
+
const items = response.kind === 'items' || response.kind === 'session-evidence'
|
|
91
|
+
? response.result.data
|
|
92
|
+
: response.kind === 'item-detail' && response.result
|
|
93
|
+
? [response.result.item]
|
|
94
|
+
: [];
|
|
95
|
+
control.confirmSavedSteers(command.nativeId, command.workspaceRoot, items);
|
|
96
|
+
return response;
|
|
97
|
+
},
|
|
89
98
|
readModels: (command) => reader.readModels(command),
|
|
90
99
|
readConfiguration: (command, nativeId, call) => nativeId
|
|
91
100
|
? control.readConfiguration(command, nativeId)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IConfigurationRequest, ISessionSettings, ITurnSettings } from '../../../contracts/index.js';
|
|
2
2
|
import type { IProviderInputFile } from '../../inputs/index.js';
|
|
3
|
-
import type { IClaudeControlEvents } from './types.js';
|
|
3
|
+
import type { IClaudeControlEvents, IClaudeSavedInput } from './types.js';
|
|
4
4
|
/**
|
|
5
5
|
* Owns one persistent native CLI per explicitly controlled Claude session.
|
|
6
6
|
* @returns Controls whose processes outlive client connections and stop at runtime shutdown.
|
|
@@ -13,6 +13,8 @@ export declare const createClaudeControl: (additionalDirectories?: string[]) =>
|
|
|
13
13
|
* Installs the runtime-owned evidence channel.
|
|
14
14
|
*/
|
|
15
15
|
setEvents(handlers: IClaudeControlEvents): void;
|
|
16
|
+
/** Settles pending native waits from exact saved input without starting another turn. */
|
|
17
|
+
confirmSavedSteers(nativeId: string, workspaceRoot: string, items: IClaudeSavedInput[]): void;
|
|
16
18
|
/**
|
|
17
19
|
* Starts an idle native session with normal local instructions and settings.
|
|
18
20
|
* @returns The exact native session identity and workspace.
|
|
@@ -54,10 +56,10 @@ export declare const createClaudeControl: (additionalDirectories?: string[]) =>
|
|
|
54
56
|
readSkills(nativeId: string): Promise<{
|
|
55
57
|
invocationPrefix: string;
|
|
56
58
|
skills: {
|
|
59
|
+
path: string | null;
|
|
57
60
|
name: string;
|
|
58
61
|
scope: string | null;
|
|
59
62
|
kind: "unavailable" | "command" | "skill";
|
|
60
|
-
path: string | null;
|
|
61
63
|
description: string;
|
|
62
64
|
argumentHint: string | null;
|
|
63
65
|
isEnabled: boolean;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { hashValue } from 'jg-crypto';
|
|
2
3
|
import { Exception } from 'jg-exception';
|
|
3
4
|
import { delay, isEqual } from 'jg-utilities';
|
|
4
5
|
import { z } from 'zod';
|
|
@@ -322,6 +323,32 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
322
323
|
setEvents(handlers) {
|
|
323
324
|
events = handlers;
|
|
324
325
|
},
|
|
326
|
+
/** Settles pending native waits from exact saved input without starting another turn. */
|
|
327
|
+
confirmSavedSteers(nativeId, workspaceRoot, items) {
|
|
328
|
+
const session = sessions.get(nativeId);
|
|
329
|
+
if (!session || session.workspaceRoot !== workspaceRoot)
|
|
330
|
+
return;
|
|
331
|
+
for (const item of items) {
|
|
332
|
+
const inputId = item.provenance.nativeInputId;
|
|
333
|
+
const steer = inputId === null ? undefined : session.steers.get(inputId);
|
|
334
|
+
const turnId = 'reference' in item ? item.reference.turnId : item.turnId;
|
|
335
|
+
if (!steer ||
|
|
336
|
+
steer.isAcknowledged ||
|
|
337
|
+
item.kind !== 'user-message' ||
|
|
338
|
+
item.provenance.nativeInputSha256 !== steer.inputSha256 ||
|
|
339
|
+
(turnId !== steer.turnId && turnId !== steer.id))
|
|
340
|
+
continue;
|
|
341
|
+
// a later live replay still needs its exact identity to start its own native turn
|
|
342
|
+
steer.isAcknowledged = true;
|
|
343
|
+
steer.resolve(steer.turnId);
|
|
344
|
+
events?.outcome({
|
|
345
|
+
kind: 'steer-accepted',
|
|
346
|
+
actionId: steer.id,
|
|
347
|
+
phase: 'steer',
|
|
348
|
+
nativeTurnId: steer.turnId,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
},
|
|
325
352
|
/**
|
|
326
353
|
* Starts an idle native session with normal local instructions and settings.
|
|
327
354
|
* @returns The exact native session identity and workspace.
|
|
@@ -494,7 +521,14 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
494
521
|
unavailable();
|
|
495
522
|
return new Promise((resolve, reject) => {
|
|
496
523
|
const id = parsedId.data;
|
|
497
|
-
session.steers.set(id, {
|
|
524
|
+
session.steers.set(id, {
|
|
525
|
+
id,
|
|
526
|
+
turnId,
|
|
527
|
+
inputSha256: hashValue(text),
|
|
528
|
+
isAcknowledged: false,
|
|
529
|
+
resolve,
|
|
530
|
+
reject,
|
|
531
|
+
});
|
|
498
532
|
void session
|
|
499
533
|
.connection.write({
|
|
500
534
|
type: 'user',
|
|
@@ -13,7 +13,7 @@ export declare const getActiveTurn: (session: IClaudeSession) => IClaudeTurn | u
|
|
|
13
13
|
*/
|
|
14
14
|
export declare const setBackgroundCommands: (session: IClaudeSession, count: number, events: IClaudeControlEvents | undefined) => void;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
16
|
+
* Releases retained steering identities and rejects any input still unconfirmed when it stops.
|
|
17
17
|
* @param session The session.
|
|
18
18
|
* @param turnId The turn whose steers fail, or every pending steer when omitted.
|
|
19
19
|
*/
|
|
@@ -156,7 +156,7 @@ export const setBackgroundCommands = (session, count, events) => {
|
|
|
156
156
|
events?.background(session.id, count);
|
|
157
157
|
};
|
|
158
158
|
/**
|
|
159
|
-
*
|
|
159
|
+
* Releases retained steering identities and rejects any input still unconfirmed when it stops.
|
|
160
160
|
* @param session The session.
|
|
161
161
|
* @param turnId The turn whose steers fail, or every pending steer when omitted.
|
|
162
162
|
*/
|
|
@@ -166,7 +166,8 @@ export const failSteers = (session, turnId) => {
|
|
|
166
166
|
continue;
|
|
167
167
|
clearTimeout(steer.timer);
|
|
168
168
|
session.steers.delete(steer.id);
|
|
169
|
-
steer.
|
|
169
|
+
if (!steer.isAcknowledged)
|
|
170
|
+
steer.reject(HQ_ERRORS.create('claudeFailed', { data: { kind: 'agent-unavailable' } }));
|
|
170
171
|
}
|
|
171
172
|
};
|
|
172
173
|
/*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IHistoryItem, ISessionSettings } from '../../../contracts/index.js';
|
|
1
|
+
import type { IEvidenceItem, IHistoryItem, ISessionSettings } from '../../../contracts/index.js';
|
|
2
2
|
import type { INativeObservation } from '../../../store/control/index.js';
|
|
3
3
|
import type { INativePrompt, IProviderSkills, IAgentProviderOutcome } from '../../index.js';
|
|
4
4
|
export type IClaudeControlEvents = {
|
|
@@ -33,10 +33,13 @@ export type IClaudeNativeTurn = {
|
|
|
33
33
|
export type IClaudeSteer = {
|
|
34
34
|
id: string;
|
|
35
35
|
turnId: string;
|
|
36
|
+
inputSha256: string;
|
|
37
|
+
isAcknowledged: boolean;
|
|
36
38
|
resolve(turnId: string): void;
|
|
37
39
|
reject(error: unknown): void;
|
|
38
40
|
timer?: ReturnType<typeof setTimeout>;
|
|
39
41
|
};
|
|
42
|
+
export type IClaudeSavedInput = Pick<IHistoryItem, 'kind' | 'provenance'> & (Pick<IHistoryItem, 'turnId'> | Pick<IEvidenceItem, 'reference'>);
|
|
40
43
|
export type IClaudeTurn = IClaudeInput | IClaudeNativeTurn;
|
|
41
44
|
export type IClaudeFastMode = {
|
|
42
45
|
state: 'on' | 'cooldown' | 'off';
|
|
@@ -107,7 +107,8 @@ export const createCodexEvents = (native, events, observeSession, observeLoaded)
|
|
|
107
107
|
const captured = turns.get(threadId);
|
|
108
108
|
if (captured?.turnId === turnId &&
|
|
109
109
|
method === 'item/completed' &&
|
|
110
|
-
item.type === 'agentMessage'
|
|
110
|
+
item.type === 'agentMessage' &&
|
|
111
|
+
item.delivery !== 'async') {
|
|
111
112
|
const message = parseNative(z.object({ text: z.string(), phase: z.string().nullish() }), item);
|
|
112
113
|
if (message.phase === undefined ||
|
|
113
114
|
message.phase === null ||
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import { UsageSnapshotSchema, UsageCreditsSchema, ThreadSummarySchema, HistoryItemSchema, MAX_ITEM_IMAGES, MAX_ITEM_TEXT_LENGTH, } from '../../contracts/index.js';
|
|
2
|
+
import { UsageSnapshotSchema, UsageCreditsSchema, ThreadSummarySchema, HistoryItemSchema, AsyncQuestionSchema, AsyncInputSchema, MAX_ITEM_IMAGES, MAX_ITEM_TEXT_LENGTH, } from '../../contracts/index.js';
|
|
3
3
|
import { countLines, describeBase64Image, keepTextEnd, toWholeFileDiff, } from '../../sessions/index.js';
|
|
4
4
|
import { nativeProvenance } from '../../sessions/evidence/index.js';
|
|
5
5
|
import { TurnSummarySchema, } from './types.js';
|
|
@@ -70,6 +70,22 @@ const TextPartsSchema = z.object({
|
|
|
70
70
|
.max(128),
|
|
71
71
|
});
|
|
72
72
|
const MessageSchema = z.object({ text: z.string() });
|
|
73
|
+
/** Preserves explicit async delivery even when its structured presentation cannot be trusted. */
|
|
74
|
+
const toAsyncInput = (item) => {
|
|
75
|
+
if (item.type !== 'agentMessage' || item.delivery !== 'async')
|
|
76
|
+
return undefined;
|
|
77
|
+
const questions = z.array(AsyncQuestionSchema).min(1).max(32).safeParse(item.questions);
|
|
78
|
+
if (!questions.success)
|
|
79
|
+
return { delivery: 'async', state: 'unavailable', reason: 'invalid' };
|
|
80
|
+
const input = AsyncInputSchema.safeParse({
|
|
81
|
+
delivery: 'async',
|
|
82
|
+
state: 'available',
|
|
83
|
+
questions: questions.data,
|
|
84
|
+
});
|
|
85
|
+
return input.success
|
|
86
|
+
? input.data
|
|
87
|
+
: { delivery: 'async', state: 'unavailable', reason: 'too-large' };
|
|
88
|
+
};
|
|
73
89
|
const StatusSchema = z.object({ status: z.string() });
|
|
74
90
|
const CommandSchema = z.object({
|
|
75
91
|
command: z.string(),
|
|
@@ -198,6 +214,9 @@ export const toHistoryItem = ({ turnId, item, startedAtMs, completedAtMs }, full
|
|
|
198
214
|
provenance: nativeProvenance(kind, kind === 'user-message' && typeof item.clientId === 'string' ? item.clientId : null, inputText),
|
|
199
215
|
text: text.slice(0, end),
|
|
200
216
|
isTruncated: end < text.length,
|
|
217
|
+
...(item.type === 'agentMessage' && item.delivery === 'async'
|
|
218
|
+
? { asyncInput: toAsyncInput(item) }
|
|
219
|
+
: {}),
|
|
201
220
|
// a Codex item holds its call and its result together
|
|
202
221
|
callId: null,
|
|
203
222
|
outcome,
|
|
@@ -59,8 +59,8 @@ export declare const ProviderInspectionSchema: z.ZodObject<{
|
|
|
59
59
|
}>;
|
|
60
60
|
managedId: z.ZodNullable<z.ZodUUID>;
|
|
61
61
|
historyState: z.ZodEnum<{
|
|
62
|
-
unavailable: "unavailable";
|
|
63
62
|
available: "available";
|
|
63
|
+
unavailable: "unavailable";
|
|
64
64
|
}>;
|
|
65
65
|
terminal: z.ZodNullable<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
66
66
|
kind: z.ZodLiteral<"interactive">;
|
|
@@ -311,6 +311,7 @@ export type INativePrompt = {
|
|
|
311
311
|
export declare const ProviderSkillsSchema: z.ZodObject<{
|
|
312
312
|
invocationPrefix: z.ZodString;
|
|
313
313
|
skills: z.ZodArray<z.ZodObject<{
|
|
314
|
+
path: z.ZodNullable<z.ZodString>;
|
|
314
315
|
name: z.ZodString;
|
|
315
316
|
scope: z.ZodNullable<z.ZodString>;
|
|
316
317
|
kind: z.ZodEnum<{
|
|
@@ -318,7 +319,6 @@ export declare const ProviderSkillsSchema: z.ZodObject<{
|
|
|
318
319
|
command: "command";
|
|
319
320
|
skill: "skill";
|
|
320
321
|
}>;
|
|
321
|
-
path: z.ZodNullable<z.ZodString>;
|
|
322
322
|
description: z.ZodString;
|
|
323
323
|
argumentHint: z.ZodNullable<z.ZodString>;
|
|
324
324
|
isEnabled: z.ZodBoolean;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { IAsyncInputEvent, IAsyncInputListener } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Delivers trusted observations without a transcript store or a callback queue. A host listener
|
|
4
|
+
* synchronously enqueues its own work; a failure requests catch-up instead of stopping control.
|
|
5
|
+
* @returns Subscription, publication and shutdown operations owned by one runtime.
|
|
6
|
+
*/
|
|
7
|
+
export declare const createAsyncInputs: () => {
|
|
8
|
+
/** Requests bounded history catch-up immediately; repeated subscriptions are independent. */
|
|
9
|
+
subscribe(listener: IAsyncInputListener): () => void;
|
|
10
|
+
/** Reports whether native event handling needs to resolve a controlled source. */
|
|
11
|
+
hasListeners(): boolean;
|
|
12
|
+
/** Delivers independent copies so one host cannot mutate another host's observation. */
|
|
13
|
+
emit(event: IAsyncInputEvent): void;
|
|
14
|
+
/** Releases all callbacks before the runtime closes its providers and storage. */
|
|
15
|
+
close(): void;
|
|
16
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delivers trusted observations without a transcript store or a callback queue. A host listener
|
|
3
|
+
* synchronously enqueues its own work; a failure requests catch-up instead of stopping control.
|
|
4
|
+
* @returns Subscription, publication and shutdown operations owned by one runtime.
|
|
5
|
+
*/
|
|
6
|
+
export const createAsyncInputs = () => {
|
|
7
|
+
const listeners = new Map();
|
|
8
|
+
let isClosed = false;
|
|
9
|
+
const deliver = (listener, event) => {
|
|
10
|
+
const subscription = listeners.get(listener);
|
|
11
|
+
if (!subscription)
|
|
12
|
+
return;
|
|
13
|
+
const recovery = {
|
|
14
|
+
kind: 'recovery',
|
|
15
|
+
provider: event.kind === 'item' ? event.source.provider : event.provider,
|
|
16
|
+
source: null,
|
|
17
|
+
reason: 'listener-failed',
|
|
18
|
+
};
|
|
19
|
+
try {
|
|
20
|
+
if (subscription.needsRecovery) {
|
|
21
|
+
listener(structuredClone(recovery));
|
|
22
|
+
subscription.needsRecovery = false;
|
|
23
|
+
}
|
|
24
|
+
if (listeners.has(listener))
|
|
25
|
+
listener(structuredClone(event));
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
subscription.needsRecovery = true;
|
|
29
|
+
try {
|
|
30
|
+
listener(structuredClone(recovery));
|
|
31
|
+
subscription.needsRecovery = false;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// retry the recovery signal before the next observation, without buffering bodies
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
/** Requests bounded history catch-up immediately; repeated subscriptions are independent. */
|
|
40
|
+
subscribe(listener) {
|
|
41
|
+
if (isClosed)
|
|
42
|
+
return () => undefined;
|
|
43
|
+
// each registration owns its wrapper, so subscribing the same callback twice is safe
|
|
44
|
+
const registered = (event) => listener(event);
|
|
45
|
+
listeners.set(registered, { needsRecovery: false });
|
|
46
|
+
deliver(registered, {
|
|
47
|
+
kind: 'recovery',
|
|
48
|
+
provider: 'codex',
|
|
49
|
+
source: null,
|
|
50
|
+
reason: 'subscribed',
|
|
51
|
+
});
|
|
52
|
+
return () => {
|
|
53
|
+
listeners.delete(registered);
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
/** Reports whether native event handling needs to resolve a controlled source. */
|
|
57
|
+
hasListeners() {
|
|
58
|
+
return listeners.size > 0;
|
|
59
|
+
},
|
|
60
|
+
/** Delivers independent copies so one host cannot mutate another host's observation. */
|
|
61
|
+
emit(event) {
|
|
62
|
+
if (isClosed)
|
|
63
|
+
return;
|
|
64
|
+
for (const listener of [...listeners.keys()])
|
|
65
|
+
deliver(listener, event);
|
|
66
|
+
},
|
|
67
|
+
/** Releases all callbacks before the runtime closes its providers and storage. */
|
|
68
|
+
close() {
|
|
69
|
+
isClosed = true;
|
|
70
|
+
listeners.clear();
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IHistoryItem, IProviderId } from '../../contracts/index.js';
|
|
2
|
+
export type IAsyncInputSource = {
|
|
3
|
+
threadId: string;
|
|
4
|
+
provider: IProviderId;
|
|
5
|
+
nativeId: string;
|
|
6
|
+
workspaceRoot: string;
|
|
7
|
+
};
|
|
8
|
+
export type IAsyncInputEvent = {
|
|
9
|
+
kind: 'item';
|
|
10
|
+
source: IAsyncInputSource;
|
|
11
|
+
item: IHistoryItem;
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'recovery';
|
|
14
|
+
provider: IProviderId;
|
|
15
|
+
source: IAsyncInputSource | null;
|
|
16
|
+
reason: 'subscribed' | 'reconnect' | 'output-gap' | 'listener-failed';
|
|
17
|
+
};
|
|
18
|
+
export type IAsyncInputListener = (event: IAsyncInputEvent) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/runtime/index.d.ts
CHANGED
package/dist/runtime/index.js
CHANGED
package/dist/runtime/runtime.js
CHANGED
|
@@ -18,6 +18,7 @@ import { HQ_ERRORS, isCleanupUnconfirmed } from '../errors/index.js';
|
|
|
18
18
|
import { claimRuntimeOwner, prepareDatabase, prepareActionKey, prepareStateRoot, recoverSnapshotLock, resolveAllowedRoots, resolveWorkspaceTarget, } from './state/index.js';
|
|
19
19
|
import { createBrowserActions } from './browser/index.js';
|
|
20
20
|
import { startWarmUp } from './warm-up/index.js';
|
|
21
|
+
import { createAsyncInputs } from './async-inputs/index.js';
|
|
21
22
|
import { AgentHqOptionsSchema } from './types.js';
|
|
22
23
|
/**
|
|
23
24
|
* Starts one local controller with private IPC, a SQLite worker and native providers, which it
|
|
@@ -96,7 +97,8 @@ export const createAgentHq = async (options) => {
|
|
|
96
97
|
const skills = createSkills(providers, projection, allowedRoots);
|
|
97
98
|
// terminal and background Claude sessions are observed while the runtime runs
|
|
98
99
|
const claudeAgents = observeClaudeAgents((sessions) => storage.background(() => projection.call('sessions', 'observe', 'claude', sessions, true)), allowedRoots, (nativeId) => projection.call('sessions', 'claudeTranscriptRoot', nativeId));
|
|
99
|
-
const
|
|
100
|
+
const asyncInputs = createAsyncInputs();
|
|
101
|
+
const actions = createActions(providers, projection, allowedRoots, ownership.epoch, actionKey, skills, storage.background, (nativeId) => claudeAgents.terminalOf(nativeId), asyncInputs);
|
|
100
102
|
// a long-poll on session states reads again once a worker call recorded a new transition
|
|
101
103
|
projection.onTransition(() => actions.polls.notify({ kind: 'transitions' }));
|
|
102
104
|
/*
|
|
@@ -129,6 +131,7 @@ export const createAgentHq = async (options) => {
|
|
|
129
131
|
let warmUp;
|
|
130
132
|
const close = () => (closing ??= (async () => {
|
|
131
133
|
try {
|
|
134
|
+
asyncInputs.close();
|
|
132
135
|
// receipt waits and long-polls end first, so none holds up closing for its remaining time
|
|
133
136
|
actions.waits.close();
|
|
134
137
|
actions.polls.close();
|
|
@@ -280,7 +283,10 @@ export const createAgentHq = async (options) => {
|
|
|
280
283
|
return { kind: 'locks', result: { data: locks, isTruncated } };
|
|
281
284
|
}
|
|
282
285
|
default: {
|
|
283
|
-
const response = await readNativeHistory(command, providers, projection, allowedRoots, claudeSessions)
|
|
286
|
+
const response = await readNativeHistory(command, providers, projection, allowedRoots, claudeSessions, (threadId, nativeId) => {
|
|
287
|
+
actions.waits.notifyThread(threadId);
|
|
288
|
+
actions.live.invalidateProvenance(JSON.stringify(['claude', nativeId]));
|
|
289
|
+
});
|
|
284
290
|
if (command.kind !== 'items' || response.kind !== 'items')
|
|
285
291
|
return response;
|
|
286
292
|
// live items reconcile against the persisted text before it is cut
|
|
@@ -317,6 +323,7 @@ export const createAgentHq = async (options) => {
|
|
|
317
323
|
runtimeEpoch: ownership.epoch,
|
|
318
324
|
closed,
|
|
319
325
|
request,
|
|
326
|
+
observeAsyncInputs: asyncInputs.subscribe,
|
|
320
327
|
get storage() {
|
|
321
328
|
return structuredClone(projection.storage);
|
|
322
329
|
},
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { z } from 'zod';
|
|
|
2
2
|
import type { IHqRequestInput, IHqResponseOf, IBrowserAction, IBrowserReceipt } from '../contracts/index.js';
|
|
3
3
|
import type { IHqSnapshotInspection } from '../snapshots/index.js';
|
|
4
4
|
import type { IStorageDescriptor, IStorageMaintenanceOperation } from '../storage/index.js';
|
|
5
|
+
import type { IAsyncInputListener } from './async-inputs/index.js';
|
|
5
6
|
export declare const AgentHqOptionsSchema: z.ZodObject<{
|
|
6
7
|
stateRoot: z.ZodString;
|
|
7
8
|
allowedRoots: z.ZodArray<z.ZodString>;
|
|
@@ -18,6 +19,14 @@ export type IPreviousRuntimeOwner = {
|
|
|
18
19
|
export type IPreviousRuntimeCheck = (owner: IPreviousRuntimeOwner) => Promise<boolean>;
|
|
19
20
|
export type IAgentHq = {
|
|
20
21
|
readonly storage: IStorageDescriptor;
|
|
22
|
+
/**
|
|
23
|
+
* Observes complete controlled-session async items independently of browser reads. The listener
|
|
24
|
+
* synchronously enqueues bounded host work. Recovery signals request native-history catch-up;
|
|
25
|
+
* observations grant no authority. Exceptions are isolated and request recovery. No replay log
|
|
26
|
+
* is retained. Runtime shutdown removes all subscriptions.
|
|
27
|
+
* @returns An idempotent unsubscribe function; a closed runtime retains no listener.
|
|
28
|
+
*/
|
|
29
|
+
observeAsyncInputs(listener: IAsyncInputListener): () => void;
|
|
21
30
|
/**
|
|
22
31
|
* Drains the runtime's admitted work and runs a host callback under exclusive maintenance
|
|
23
32
|
* admission. The callback owns its SQLite connection; its errors propagate unchanged.
|
|
@@ -9,10 +9,14 @@ type IProvenanceItem = Pick<IHistoryItem, 'kind' | 'provenance'> & (Pick<IHistor
|
|
|
9
9
|
* @param provider The provider whose native session supplied the rows.
|
|
10
10
|
* @param nativeId The selected native session.
|
|
11
11
|
* @param store The authoritative action ledger.
|
|
12
|
+
* @param persisted Authorized saved history can confirm an exact Claude steer without a replay.
|
|
12
13
|
* @mutates Replaces provenance on the supplied presentation rows.
|
|
13
14
|
* @returns Completion after the eligible user inputs have been correlated.
|
|
14
15
|
* @throws
|
|
15
16
|
* - JG_AGENT_HQ_STORE_FAILED: The HQ store failed
|
|
16
17
|
*/
|
|
17
|
-
export declare const correlateEvidenceOrigins: (items: IProvenanceItem[], provider: IProviderId, nativeId: string, store: IStore
|
|
18
|
+
export declare const correlateEvidenceOrigins: (items: IProvenanceItem[], provider: IProviderId, nativeId: string, store: IStore, persisted?: {
|
|
19
|
+
workspaceRoot: string;
|
|
20
|
+
onAcknowledged(threadId: string): void;
|
|
21
|
+
}) => Promise<void>;
|
|
18
22
|
export {};
|
|
@@ -8,12 +8,13 @@ import { resetProvenance } from './utilities.js';
|
|
|
8
8
|
* @param provider The provider whose native session supplied the rows.
|
|
9
9
|
* @param nativeId The selected native session.
|
|
10
10
|
* @param store The authoritative action ledger.
|
|
11
|
+
* @param persisted Authorized saved history can confirm an exact Claude steer without a replay.
|
|
11
12
|
* @mutates Replaces provenance on the supplied presentation rows.
|
|
12
13
|
* @returns Completion after the eligible user inputs have been correlated.
|
|
13
14
|
* @throws
|
|
14
15
|
* - JG_AGENT_HQ_STORE_FAILED: The HQ store failed
|
|
15
16
|
*/
|
|
16
|
-
export const correlateEvidenceOrigins = async (items, provider, nativeId, store) => {
|
|
17
|
+
export const correlateEvidenceOrigins = async (items, provider, nativeId, store, persisted) => {
|
|
17
18
|
for (const item of items)
|
|
18
19
|
item.provenance = resetProvenance(item.kind, item.provenance);
|
|
19
20
|
const inputs = items.filter(({ kind, provenance }) => kind === 'user-message' &&
|
|
@@ -21,7 +22,7 @@ export const correlateEvidenceOrigins = async (items, provider, nativeId, store)
|
|
|
21
22
|
provenance.nativeInputSha256 !== null);
|
|
22
23
|
if (inputs.length === 0)
|
|
23
24
|
return;
|
|
24
|
-
const
|
|
25
|
+
const command = {
|
|
25
26
|
provider,
|
|
26
27
|
nativeId,
|
|
27
28
|
items: inputs.map(({ provenance, ...item }) => ({
|
|
@@ -30,7 +31,16 @@ export const correlateEvidenceOrigins = async (items, provider, nativeId, store)
|
|
|
30
31
|
turnId: 'reference' in item ? item.reference.turnId : item.turnId,
|
|
31
32
|
sha256: provenance.nativeInputSha256,
|
|
32
33
|
})),
|
|
33
|
-
}
|
|
34
|
+
};
|
|
35
|
+
if (persisted && provider === 'claude') {
|
|
36
|
+
const threads = await store.call('outcomes', 'confirmClaudeSteers', {
|
|
37
|
+
...command,
|
|
38
|
+
workspaceRoot: persisted.workspaceRoot,
|
|
39
|
+
});
|
|
40
|
+
for (const threadId of threads)
|
|
41
|
+
persisted.onAcknowledged(threadId);
|
|
42
|
+
}
|
|
43
|
+
const origins = await store.call('reads', 'getEvidenceOrigins', command);
|
|
34
44
|
for (const [index, origin] of origins.entries())
|
|
35
45
|
if (origin)
|
|
36
46
|
inputs[index].provenance = origin;
|
|
@@ -130,6 +130,7 @@ export const buildEvidencePage = (command, position, items, nextSourceCursor) =>
|
|
|
130
130
|
},
|
|
131
131
|
kind: item.kind,
|
|
132
132
|
nativeType: item.nativeType,
|
|
133
|
+
...(item.asyncInput ? { asyncInput: item.asyncInput } : {}),
|
|
133
134
|
provenance: item.provenance,
|
|
134
135
|
text,
|
|
135
136
|
offsetBytes: offset,
|
|
@@ -19,7 +19,7 @@ import type { IClaudeSessions, IHistoryRequest } from './types.js';
|
|
|
19
19
|
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
20
20
|
* - JG_AGENT_HQ_STORE_FAILED: The HQ store failed
|
|
21
21
|
*/
|
|
22
|
-
export declare const readNativeHistory: (command: IHistoryRequest, registry: ReturnType<typeof createProviderRegistry>, projection: IStore, allowedRoots: string[], claude: IClaudeSessions) => Promise<IHqResponse>;
|
|
22
|
+
export declare const readNativeHistory: (command: IHistoryRequest, registry: ReturnType<typeof createProviderRegistry>, projection: IStore, allowedRoots: string[], claude: IClaudeSessions, onInputAcknowledged: (threadId: string, nativeId: string) => void) => Promise<IHqResponse>;
|
|
23
23
|
/**
|
|
24
24
|
* Reads every running and waiting session, managed and observed, and every idle one that runs
|
|
25
25
|
* background commands, newest state change first, each as the summary `threads` returns, without
|
|
@@ -21,7 +21,7 @@ import { correlateEvidenceOrigins } from './evidence/index.js';
|
|
|
21
21
|
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
22
22
|
* - JG_AGENT_HQ_STORE_FAILED: The HQ store failed
|
|
23
23
|
*/
|
|
24
|
-
export const readNativeHistory = async (command, registry, projection, allowedRoots, claude) => {
|
|
24
|
+
export const readNativeHistory = async (command, registry, projection, allowedRoots, claude, onInputAcknowledged) => {
|
|
25
25
|
const workspaceRoot = await resolveWorkspaceTarget(command.workspaceRoot, allowedRoots);
|
|
26
26
|
// a thread HQ created has no native history until its first turn, so its record answers
|
|
27
27
|
if (command.kind === 'thread' ||
|
|
@@ -71,7 +71,10 @@ export const readNativeHistory = async (command, registry, projection, allowedRo
|
|
|
71
71
|
? [response.result.item]
|
|
72
72
|
: [];
|
|
73
73
|
if (items.length && 'nativeId' in command)
|
|
74
|
-
await correlateEvidenceOrigins(items, command.provider, command.nativeId, projection
|
|
74
|
+
await correlateEvidenceOrigins(items, command.provider, command.nativeId, projection, {
|
|
75
|
+
workspaceRoot,
|
|
76
|
+
onAcknowledged: (threadId) => onInputAcknowledged(threadId, command.nativeId),
|
|
77
|
+
});
|
|
75
78
|
if (response.kind === 'session-evidence' && Buffer.byteLength(JSON.stringify(response)) > 65_280)
|
|
76
79
|
throw HQ_ERRORS.create('providerFailed', { data: { kind: 'agent-unavailable' } });
|
|
77
80
|
return response;
|
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import type { IActionReceipt } from '../../../contracts/index.js';
|
|
2
|
+
import type { IEvidenceOriginsRequest } from '../../../sessions/evidence/index.js';
|
|
2
3
|
import { ControlModel } from '../control.model.js';
|
|
3
4
|
import type { INativeOutcome } from '../types.js';
|
|
4
5
|
/** Applies native request outcomes to the actions whose phases they answer. */
|
|
5
6
|
export declare class ControlOutcomes extends ControlModel {
|
|
7
|
+
/**
|
|
8
|
+
* Confirms saved Claude input against its exact admitted steer, including an earlier runtime's
|
|
9
|
+
* uncertain receipt. This changes delivery only; it never restores control or dispatches work.
|
|
10
|
+
* @returns The thread IDs whose receipt changed.
|
|
11
|
+
*/
|
|
12
|
+
confirmClaudeSteers(command: IEvidenceOriginsRequest & {
|
|
13
|
+
workspaceRoot: string;
|
|
14
|
+
}): string[];
|
|
6
15
|
/**
|
|
7
16
|
* Applies an exact native request outcome without downgrading a known acknowledgement. An
|
|
8
17
|
* outcome answers the phase its action takes now; one for a phase the action has left, or from
|
|
@@ -2,6 +2,33 @@ import { ControlModel } from '../control.model.js';
|
|
|
2
2
|
import { toActionReceipt } from '../transformers.js';
|
|
3
3
|
/** Applies native request outcomes to the actions whose phases they answer. */
|
|
4
4
|
export class ControlOutcomes extends ControlModel {
|
|
5
|
+
/**
|
|
6
|
+
* Confirms saved Claude input against its exact admitted steer, including an earlier runtime's
|
|
7
|
+
* uncertain receipt. This changes delivery only; it never restores control or dispatches work.
|
|
8
|
+
* @returns The thread IDs whose receipt changed.
|
|
9
|
+
*/
|
|
10
|
+
confirmClaudeSteers(command) {
|
|
11
|
+
if (command.provider !== 'claude')
|
|
12
|
+
return [];
|
|
13
|
+
return this._transaction(() => {
|
|
14
|
+
const threads = new Set();
|
|
15
|
+
for (const item of command.items) {
|
|
16
|
+
if (item.nativeInputId === null || item.sha256 === null)
|
|
17
|
+
continue;
|
|
18
|
+
const updated = this._statements.get(`UPDATE agent_hq_action SET delivery = 'acknowledged', failure = NULL, updated_at = ?
|
|
19
|
+
WHERE id = ? AND provider = 'claude' AND kind = 'steer' AND phase = 'steer'
|
|
20
|
+
AND delivery IN ('submitted', 'uncertain') AND input_sha256 = ?
|
|
21
|
+
AND native_turn_id IS NOT NULL AND native_turn_id = expected_turn_id
|
|
22
|
+
AND (native_turn_id = ? OR id = ?)
|
|
23
|
+
AND thread_id IN (SELECT id FROM agent_hq_thread
|
|
24
|
+
WHERE provider = 'claude' AND native_id = ? AND workspace_root = ?)
|
|
25
|
+
RETURNING thread_id`, Date.now(), item.nativeInputId, item.sha256, item.turnId, item.turnId, command.nativeId, command.workspaceRoot);
|
|
26
|
+
if (updated)
|
|
27
|
+
threads.add(updated.thread_id);
|
|
28
|
+
}
|
|
29
|
+
return [...threads];
|
|
30
|
+
});
|
|
31
|
+
}
|
|
5
32
|
/**
|
|
6
33
|
* Applies an exact native request outcome without downgrading a known acknowledgement. An
|
|
7
34
|
* outcome answers the phase its action takes now; one for a phase the action has left, or from
|
package/docs/recovery.md
CHANGED
|
@@ -20,6 +20,8 @@ The recovered runtime rebuilds the Claude history index from the transcripts on
|
|
|
20
20
|
|
|
21
21
|
Recover repositories and native files separately. Source paths are retained as evidence; never blindly replace repository files. Reconcile native ownership before enabling execution. The snapshot does not promise path translation or restore authentication.
|
|
22
22
|
|
|
23
|
+
A saved Claude input can resolve an uncertain steering receipt when an authorized history, detail or evidence read verifies its exact action, session, workspace, turn and original-text fingerprint. This confirms delivery only. It does not recover ownership, resume a session, resend input or release queued work.
|
|
24
|
+
|
|
23
25
|
## Rollback
|
|
24
26
|
|
|
25
27
|
Roll back by restoring an earlier snapshot into a new recovery workspace; never overwrite the current state root. A snapshot from another schema is refused with an error naming the package version that wrote it, so restore it with that version. Rollback accepts losing every change made after that snapshot, including host changes and later runtime receipts. Reconcile native ownership before enabling work.
|
|
@@ -32,6 +34,8 @@ The expanded name contract is still a breaking change for older consumers: 12.x
|
|
|
32
34
|
|
|
33
35
|
## Codex connection recovery
|
|
34
36
|
|
|
37
|
+
Async question subscriptions are ephemeral. Recreate the host's `observeAsyncInputs` subscription after startup and recover from authorized bounded native history when it requests catch-up. Persist host resolution/checkpoint metadata in application-owned tables; the package stores neither question bodies nor an observation replay log. Deduplicate exact session/turn/item identities, preserve unresolved questions after turn completion, and never resend an answer merely because its observation or acknowledgement was lost. Snapshot rollback can lose host resolution metadata even when native input already arrived, so reconcile native action evidence before allowing a replacement answer. This feature does not change the package storage stamp or action key.
|
|
38
|
+
|
|
35
39
|
A quarantined session can still be monitored while a CLI runs it. Native observations update its live state without recovering ownership; the summary keeps uncertain ownership and its existing managed identity. Monitoring sends no input and leaves the control ledger and earlier queued entries unchanged.
|
|
36
40
|
|
|
37
41
|
A shared Codex daemon restart can interrupt every managed Codex session's live connection while HQ stays running. The runtime rechecks only its previously controlled sessions with no action in flight and rejoins matching native turns without changing settings or repeating input. Confirmed completions advance their waiting queues; unacknowledged actions and changed turns remain uncertain. Native command handles may have been lost, so rejoining establishes session control rather than successful command execution. See [runtime lifecycle](../api/README.md#runtime-lifecycle).
|