jg-agent-hq 10.2.0 → 10.4.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 +20 -0
- package/README.md +1 -1
- package/api/README.md +36 -15
- package/dist/actions/actions.d.ts +2 -1
- package/dist/actions/actions.js +211 -14
- package/dist/actions/resync/constants.d.ts +3 -0
- package/dist/actions/resync/constants.js +9 -0
- package/dist/actions/resync/index.d.ts +4 -0
- package/dist/actions/resync/index.js +6 -0
- package/dist/actions/resync/resync.d.ts +24 -0
- package/dist/actions/resync/resync.js +63 -0
- package/dist/actions/resync/types.d.ts +22 -0
- package/dist/actions/resync/types.js +1 -0
- package/dist/actions/resync/utilities.d.ts +18 -0
- package/dist/actions/resync/utilities.js +51 -0
- package/dist/index.d.ts +2 -2
- package/dist/ipc/index.d.ts +1 -1
- package/dist/ipc/index.js +1 -1
- package/dist/ipc/server.d.ts +6 -0
- package/dist/ipc/server.js +17 -2
- package/dist/locks/index.d.ts +1 -1
- package/dist/locks/index.js +1 -1
- package/dist/locks/locks.d.ts +8 -0
- package/dist/locks/locks.js +25 -0
- package/dist/providers/claude/claude.js +1 -1
- package/dist/providers/claude/configuration/configuration.d.ts +3 -1
- package/dist/providers/claude/configuration/configuration.js +5 -3
- package/dist/providers/claude/control/control.d.ts +5 -2
- package/dist/providers/claude/control/control.js +30 -11
- package/dist/providers/claude/control/permissions.js +6 -2
- package/dist/providers/claude/control/stream.d.ts +3 -1
- package/dist/providers/claude/control/stream.js +97 -34
- package/dist/providers/claude/control/types.d.ts +9 -0
- package/dist/providers/codex/adapter.js +28 -0
- package/dist/providers/codex/codex.js +4 -0
- package/dist/providers/codex/constants.d.ts +1 -0
- package/dist/providers/codex/constants.js +6 -0
- package/dist/providers/codex/events.js +137 -107
- package/dist/providers/codex/rpc.d.ts +1 -0
- package/dist/providers/codex/rpc.js +1 -1
- package/dist/providers/codex/transformers.d.ts +6 -3
- package/dist/providers/codex/transformers.js +7 -2
- package/dist/providers/codex/types.d.ts +8 -0
- package/dist/providers/codex/types.js +5 -0
- package/dist/providers/codex/websocket/websocket.js +57 -9
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.js +1 -1
- package/dist/providers/process.d.ts +1 -1
- package/dist/providers/process.js +2 -1
- package/dist/providers/registry/registry.d.ts +2 -0
- package/dist/providers/registry/registry.js +36 -1
- package/dist/providers/registry/types.d.ts +1 -0
- package/dist/providers/types.d.ts +51 -0
- package/dist/providers/types.js +21 -0
- package/dist/runtime/index.d.ts +1 -1
- package/dist/runtime/runtime.js +16 -7
- package/dist/runtime/state/state.d.ts +7 -3
- package/dist/runtime/state/state.js +71 -9
- package/dist/runtime/types.d.ts +6 -0
- package/dist/runtime/types.js +8 -0
- package/dist/store/control/constants.d.ts +1 -0
- package/dist/store/control/constants.js +7 -0
- package/dist/store/control/control.model.d.ts +30 -4
- package/dist/store/control/control.model.js +109 -9
- package/dist/store/control/index.d.ts +2 -2
- package/dist/store/control/index.js +1 -1
- package/dist/store/control/types.d.ts +22 -0
- package/dist/store/control/types.js +25 -1
- package/dist/store/store.d.ts +1 -1
- package/dist/store/store.js +9 -3
- package/dist/store/store.worker.js +13 -1
- package/dist/store/types.d.ts +79 -1
- package/dist/store/types.js +12 -2
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { IResyncRequest } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Runs live re-checks of controlled sessions outside the shared event lane, one at a time per
|
|
4
|
+
* provider, so they hold at most one of a native connection's request slots. A session has at
|
|
5
|
+
* most one waiting re-check; another request for it merges into that one, or follows the one
|
|
6
|
+
* running.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Resyncs {
|
|
9
|
+
private readonly __providers;
|
|
10
|
+
private readonly __recheck;
|
|
11
|
+
private readonly __runOwned;
|
|
12
|
+
/**
|
|
13
|
+
* @param recheck Reads one session and applies what it finds.
|
|
14
|
+
* @param runOwned Runs a provider's re-checks in the background the runtime owns.
|
|
15
|
+
*/
|
|
16
|
+
constructor(recheck: (provider: string, nativeId: string, request: IResyncRequest) => Promise<void>, runOwned: (operation: () => Promise<void>) => void);
|
|
17
|
+
/** Asks for a re-check of one session and starts its provider's re-checks when none run. */
|
|
18
|
+
request(provider: string, nativeId: string, request: IResyncRequest): void;
|
|
19
|
+
/**
|
|
20
|
+
* Runs a provider's waiting re-checks in order until none is left.
|
|
21
|
+
* @returns A promise resolved once the provider has no waiting re-check.
|
|
22
|
+
*/
|
|
23
|
+
private __drain;
|
|
24
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { mergeResync } from './utilities.js';
|
|
2
|
+
/**
|
|
3
|
+
* Runs live re-checks of controlled sessions outside the shared event lane, one at a time per
|
|
4
|
+
* provider, so they hold at most one of a native connection's request slots. A session has at
|
|
5
|
+
* most one waiting re-check; another request for it merges into that one, or follows the one
|
|
6
|
+
* running.
|
|
7
|
+
*/
|
|
8
|
+
export class Resyncs {
|
|
9
|
+
__providers = new Map();
|
|
10
|
+
__recheck;
|
|
11
|
+
__runOwned;
|
|
12
|
+
/**
|
|
13
|
+
* @param recheck Reads one session and applies what it finds.
|
|
14
|
+
* @param runOwned Runs a provider's re-checks in the background the runtime owns.
|
|
15
|
+
*/
|
|
16
|
+
constructor(recheck, runOwned) {
|
|
17
|
+
this.__recheck = recheck;
|
|
18
|
+
this.__runOwned = runOwned;
|
|
19
|
+
}
|
|
20
|
+
/** Asks for a re-check of one session and starts its provider's re-checks when none run. */
|
|
21
|
+
request(provider, nativeId, request) {
|
|
22
|
+
let queue = this.__providers.get(provider);
|
|
23
|
+
if (!queue) {
|
|
24
|
+
queue = { waiting: new Map(), running: null, followUps: new Map(), isDraining: false };
|
|
25
|
+
this.__providers.set(provider, queue);
|
|
26
|
+
}
|
|
27
|
+
if (queue.running === nativeId)
|
|
28
|
+
queue.followUps.set(nativeId, mergeResync(queue.followUps.get(nativeId), request));
|
|
29
|
+
else
|
|
30
|
+
queue.waiting.set(nativeId, mergeResync(queue.waiting.get(nativeId), request));
|
|
31
|
+
if (queue.isDraining)
|
|
32
|
+
return;
|
|
33
|
+
queue.isDraining = true;
|
|
34
|
+
const draining = queue;
|
|
35
|
+
this.__runOwned(() => this.__drain(provider, draining));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Runs a provider's waiting re-checks in order until none is left.
|
|
39
|
+
* @returns A promise resolved once the provider has no waiting re-check.
|
|
40
|
+
*/
|
|
41
|
+
async __drain(provider, queue) {
|
|
42
|
+
try {
|
|
43
|
+
for (const [nativeId, request] of queue.waiting) {
|
|
44
|
+
queue.waiting.delete(nativeId);
|
|
45
|
+
queue.running = nativeId;
|
|
46
|
+
try {
|
|
47
|
+
await this.__recheck(provider, nativeId, request);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
queue.running = null;
|
|
51
|
+
const followUp = queue.followUps.get(nativeId);
|
|
52
|
+
if (followUp) {
|
|
53
|
+
queue.followUps.delete(nativeId);
|
|
54
|
+
queue.waiting.set(nativeId, mergeResync(queue.waiting.get(nativeId), followUp));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
queue.isDraining = false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { IProviderReattachment, IResyncLoss } from '../../providers/index.js';
|
|
2
|
+
export type IResyncRequest = {
|
|
3
|
+
lost: IResyncLoss;
|
|
4
|
+
isLastAttempt: boolean;
|
|
5
|
+
turnId: string | null;
|
|
6
|
+
};
|
|
7
|
+
export type IResyncDecision = {
|
|
8
|
+
kind: 'complete';
|
|
9
|
+
nativeTurnId: string;
|
|
10
|
+
state: Exclude<NonNullable<IProviderReattachment['latestTurn']>['state'], 'running'>;
|
|
11
|
+
} | {
|
|
12
|
+
kind: 'gap';
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'watch';
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'reread';
|
|
17
|
+
nativeTurnId: string;
|
|
18
|
+
} | {
|
|
19
|
+
kind: 'none';
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'uncertain';
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IProviderReattachment } from '../../providers/index.js';
|
|
2
|
+
import type { ITurnRecord } from '../../store/control/index.js';
|
|
3
|
+
import type { IResyncDecision, IResyncRequest } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Merges a re-check into one already waiting for the same session, keeping the most demanding lost
|
|
6
|
+
* kind. A fresh request turns a pending last attempt back into a full re-check.
|
|
7
|
+
* @returns The single request that stands for both.
|
|
8
|
+
*/
|
|
9
|
+
export declare const mergeResync: (current: IResyncRequest | undefined, next: IResyncRequest) => IResyncRequest;
|
|
10
|
+
/**
|
|
11
|
+
* Decides what a lost notification means for a session, from the turns it recorded and the latest
|
|
12
|
+
* turn its native process reports. A finished turn completes; a running one continues unless its
|
|
13
|
+
* end was lost, and is watched after an unidentified loss; an ambiguous one is read once more; an
|
|
14
|
+
* idle, unchanged session is left alone; and anything else, such as a newer turn, a lost approval
|
|
15
|
+
* resolution or a last attempt that still can't settle, makes the session uncertain.
|
|
16
|
+
* @returns The decision the re-check applies.
|
|
17
|
+
*/
|
|
18
|
+
export declare const decideResync: (record: ITurnRecord, latest: IProviderReattachment["latestTurn"], request: IResyncRequest) => IResyncDecision;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { RESYNC_LOSS_ORDER } from './constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* Merges a re-check into one already waiting for the same session, keeping the most demanding lost
|
|
4
|
+
* kind. A fresh request turns a pending last attempt back into a full re-check.
|
|
5
|
+
* @returns The single request that stands for both.
|
|
6
|
+
*/
|
|
7
|
+
export const mergeResync = (current, next) => {
|
|
8
|
+
if (!current)
|
|
9
|
+
return next;
|
|
10
|
+
const isLastAttempt = current.isLastAttempt && next.isLastAttempt;
|
|
11
|
+
return {
|
|
12
|
+
lost: RESYNC_LOSS_ORDER.indexOf(next.lost) > RESYNC_LOSS_ORDER.indexOf(current.lost)
|
|
13
|
+
? next.lost
|
|
14
|
+
: current.lost,
|
|
15
|
+
isLastAttempt,
|
|
16
|
+
turnId: isLastAttempt ? current.turnId : null,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Decides what a lost notification means for a session, from the turns it recorded and the latest
|
|
21
|
+
* turn its native process reports. A finished turn completes; a running one continues unless its
|
|
22
|
+
* end was lost, and is watched after an unidentified loss; an ambiguous one is read once more; an
|
|
23
|
+
* idle, unchanged session is left alone; and anything else, such as a newer turn, a lost approval
|
|
24
|
+
* resolution or a last attempt that still can't settle, makes the session uncertain.
|
|
25
|
+
* @returns The decision the re-check applies.
|
|
26
|
+
*/
|
|
27
|
+
export const decideResync = (record, latest, request) => {
|
|
28
|
+
const active = record.activeTurnId;
|
|
29
|
+
// the turn a last attempt reads again has been settled by other evidence meanwhile
|
|
30
|
+
if (request.isLastAttempt && active !== request.turnId)
|
|
31
|
+
return { kind: 'none' };
|
|
32
|
+
const isActiveLatest = active !== null && latest !== null && latest.nativeId === active;
|
|
33
|
+
if (isActiveLatest && latest.state !== 'running' && !latest.isInferred)
|
|
34
|
+
return { kind: 'complete', nativeTurnId: active, state: latest.state };
|
|
35
|
+
if (request.isLastAttempt)
|
|
36
|
+
return { kind: 'uncertain' };
|
|
37
|
+
if (isActiveLatest && latest.state === 'running' && request.lost === 'turn-started')
|
|
38
|
+
return { kind: 'gap' };
|
|
39
|
+
// an unidentified loss may have been this turn's end, which Codex's next idle status reveals
|
|
40
|
+
if (isActiveLatest && latest.state === 'running' && request.lost === 'unknown')
|
|
41
|
+
return { kind: 'watch' };
|
|
42
|
+
if (isActiveLatest &&
|
|
43
|
+
((latest.state === 'running' && request.lost === 'turn-completed') ||
|
|
44
|
+
(latest.isInferred === true && latest.state === 'interrupted')))
|
|
45
|
+
return { kind: 'reread', nativeTurnId: active };
|
|
46
|
+
if (active === null &&
|
|
47
|
+
(latest?.nativeId ?? null) === record.lastTurnId &&
|
|
48
|
+
latest?.state !== 'running')
|
|
49
|
+
return { kind: 'none' };
|
|
50
|
+
return { kind: 'uncertain' };
|
|
51
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { type IHqLockOwner, type IOwnerLiveness, type IHqLockOptions, type IHqLockState, RUNTIME_DIRECTORY, HQ_LOCK_FILE, getOwnerLiveness, acquireHqLock, withHqLock, releaseHqLock, readHqLockState, } from './locks/index.js';
|
|
2
2
|
export { createAgentHq } from './runtime/index.js';
|
|
3
|
-
export type { IAgentHq, IAgentHqOptions } from './runtime/index.js';
|
|
3
|
+
export type { IAgentHq, IAgentHqOptions, IPreviousRuntimeCheck, IPreviousRuntimeOwner, } from './runtime/index.js';
|
|
4
4
|
export { requestAgentHq } from './ipc/index.js';
|
|
5
|
-
export type { IAgentProvider, IAgentProviderFactory, IAgentProviderContext, IAgentProviderEvents, IAgentProviderOutcome, INativePrompt, IProviderCall, IProviderThread, IProviderInspection, IProviderSkills, IProviderMcpServerPage, IProviderMcpTools, IProviderMcpResources, } from './providers/index.js';
|
|
5
|
+
export type { IAgentProvider, IAgentProviderFactory, IAgentProviderContext, IAgentProviderEvents, IAgentProviderOutcome, INativePrompt, IProviderCall, IProviderThread, IProviderInspection, IProviderReattachment, IResyncLoss, IProviderSkills, IProviderMcpServerPage, IProviderMcpTools, IProviderMcpResources, } from './providers/index.js';
|
|
6
6
|
export type { IProviderId, IProviderCapability, IProviderCapabilitySet, IProviderCapabilities, IProviderStatus, } from './contracts/providers/index.js';
|
|
7
7
|
export type { IProviderModel, IProviderModelPage, IProviderModelsRequest, } from './contracts/models/index.js';
|
|
8
8
|
export type { ITurnSettings, ISessionSettings, IConfigurationRequest, IConfigurationChoice, ISessionConfiguration, } from './contracts/configuration/index.js';
|
package/dist/ipc/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createLineDecoder } from './framing.js';
|
|
2
2
|
export { MAX_CONNECTIONS, MAX_REQUEST_BYTES, MAX_FRAME_BYTES, IPC_TIMEOUT_MS, CONTROL_SOCKET, } from './constants.js';
|
|
3
|
-
export { createIpcServer } from './server.js';
|
|
3
|
+
export { createIpcServer, removeLeftoverSocket } from './server.js';
|
|
4
4
|
export { requestAgentHq } from './client.js';
|
package/dist/ipc/index.js
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
export { createLineDecoder } from './framing.js';
|
|
3
3
|
export { MAX_CONNECTIONS, MAX_REQUEST_BYTES, MAX_FRAME_BYTES, IPC_TIMEOUT_MS, CONTROL_SOCKET, } from './constants.js';
|
|
4
4
|
// local command connections
|
|
5
|
-
export { createIpcServer } from './server.js';
|
|
5
|
+
export { createIpcServer, removeLeftoverSocket } from './server.js';
|
|
6
6
|
export { requestAgentHq } from './client.js';
|
package/dist/ipc/server.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { type IHqRequest, type IHqResponse } from '../contracts/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Removes the socket a runtime that stopped without closing its server left at the control path,
|
|
4
|
+
* so the next server can listen there. Anything else at that path, such as a symbolic link, stays.
|
|
5
|
+
* @returns A promise resolved once no leftover socket remains.
|
|
6
|
+
*/
|
|
7
|
+
export declare const removeLeftoverSocket: (path: string) => Promise<void>;
|
|
2
8
|
/**
|
|
3
9
|
* Serves closed HQ commands over an owner-only Unix socket. Disconnects do not own host lifetime.
|
|
4
10
|
* @returns An owned server with deterministic connection teardown.
|
package/dist/ipc/server.js
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import { createServer } from 'node:net';
|
|
2
|
-
import { chmod } from 'node:fs/promises';
|
|
2
|
+
import { chmod, lstat, rm } from 'node:fs/promises';
|
|
3
3
|
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import { Exception, extractMessage } from 'jg-exception';
|
|
4
|
+
import { Exception, extractMessage, hasErrorCode } from 'jg-exception';
|
|
5
5
|
import { HqResponseSchema, IpcRequestSchema, PUBLIC_ERROR_MESSAGES, } from '../contracts/index.js';
|
|
6
6
|
import { toPublicError } from '../errors/index.js';
|
|
7
7
|
import { IPC_TIMEOUT_MS, MAX_CONNECTIONS, MAX_FRAME_BYTES, MAX_REQUEST_BYTES, } from './constants.js';
|
|
8
8
|
import { createLineDecoder } from './framing.js';
|
|
9
|
+
/**
|
|
10
|
+
* Removes the socket a runtime that stopped without closing its server left at the control path,
|
|
11
|
+
* so the next server can listen there. Anything else at that path, such as a symbolic link, stays.
|
|
12
|
+
* @returns A promise resolved once no leftover socket remains.
|
|
13
|
+
*/
|
|
14
|
+
export const removeLeftoverSocket = async (path) => {
|
|
15
|
+
try {
|
|
16
|
+
if ((await lstat(path)).isSocket())
|
|
17
|
+
await rm(path);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (!hasErrorCode(error, 'ENOENT'))
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
9
24
|
/**
|
|
10
25
|
* Serves closed HQ commands over an owner-only Unix socket. Disconnects do not own host lifetime.
|
|
11
26
|
* @returns An owned server with deterministic connection teardown.
|
package/dist/locks/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type { IHqLockOwner, IOwnerLiveness, IHqLockOptions, IHqLockState } from './types.js';
|
|
2
2
|
export { RUNTIME_DIRECTORY, HQ_LOCK_FILE } from './constants.js';
|
|
3
|
-
export { ensurePrivateRuntimeDirectory, isPrivateOwner, getOwnerLiveness, acquireHqLock, withHqLock, releaseHqLock, readHqLockState, } from './locks.js';
|
|
3
|
+
export { ensurePrivateRuntimeDirectory, isPrivateOwner, getOwnerLiveness, acquireHqLock, withHqLock, releaseHqLock, removeStaleHqLock, readHqLockState, } from './locks.js';
|
|
4
4
|
export { HqLockOwnerSchema } from './types.js';
|
package/dist/locks/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// constants
|
|
2
2
|
export { RUNTIME_DIRECTORY, HQ_LOCK_FILE } from './constants.js';
|
|
3
3
|
// lock lifecycle
|
|
4
|
-
export { ensurePrivateRuntimeDirectory, isPrivateOwner, getOwnerLiveness, acquireHqLock, withHqLock, releaseHqLock, readHqLockState, } from './locks.js';
|
|
4
|
+
export { ensurePrivateRuntimeDirectory, isPrivateOwner, getOwnerLiveness, acquireHqLock, withHqLock, releaseHqLock, removeStaleHqLock, readHqLockState, } from './locks.js';
|
|
5
5
|
// internal ownership record validation
|
|
6
6
|
export { HqLockOwnerSchema } from './types.js';
|
package/dist/locks/locks.d.ts
CHANGED
|
@@ -83,6 +83,14 @@ export declare const withHqLock: <T>(root: string, command: string, operation: (
|
|
|
83
83
|
* - JG_AGENT_HQ_LOCK_NONCE_MISMATCH: The HQ lock nonce does not match
|
|
84
84
|
*/
|
|
85
85
|
export declare const releaseHqLock: (root: string, nonce: string) => Promise<IHqLockOwner>;
|
|
86
|
+
/**
|
|
87
|
+
* Removes the HQ lock of a holder that no longer exists, once a runtime that stopped without
|
|
88
|
+
* releasing its state root proved none of its processes remain. A lock whose holder exists, can't
|
|
89
|
+
* be signaled or can't be read stays in place, and so does one that changed after it was read.
|
|
90
|
+
* @param root The trusted state root.
|
|
91
|
+
* @returns A promise that resolves to whether the lock was removed.
|
|
92
|
+
*/
|
|
93
|
+
export declare const removeStaleHqLock: (root: string) => Promise<boolean>;
|
|
86
94
|
/**
|
|
87
95
|
* Reports the HQ lock for status output without acquiring or changing it.
|
|
88
96
|
* @param root The trusted state root.
|
package/dist/locks/locks.js
CHANGED
|
@@ -347,6 +347,31 @@ export const releaseHqLock = async (root, nonce) => {
|
|
|
347
347
|
await rm(path);
|
|
348
348
|
return stored.owner;
|
|
349
349
|
};
|
|
350
|
+
/**
|
|
351
|
+
* Removes the HQ lock of a holder that no longer exists, once a runtime that stopped without
|
|
352
|
+
* releasing its state root proved none of its processes remain. A lock whose holder exists, can't
|
|
353
|
+
* be signaled or can't be read stays in place, and so does one that changed after it was read.
|
|
354
|
+
* @param root The trusted state root.
|
|
355
|
+
* @returns A promise that resolves to whether the lock was removed.
|
|
356
|
+
*/
|
|
357
|
+
export const removeStaleHqLock = async (root) => {
|
|
358
|
+
const path = join(root, RUNTIME_DIRECTORY, HQ_LOCK_FILE);
|
|
359
|
+
let stored;
|
|
360
|
+
try {
|
|
361
|
+
stored = await readStoredLock(path);
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
// an unreadable lock keeps its manual inspection
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
if (stored.kind !== 'owner' || getOwnerLiveness(stored.owner.pid) !== 'dead')
|
|
368
|
+
return false;
|
|
369
|
+
const current = await readStoredLock(path);
|
|
370
|
+
if (current.kind !== 'owner' || current.content !== stored.content)
|
|
371
|
+
return false;
|
|
372
|
+
await rm(path);
|
|
373
|
+
return true;
|
|
374
|
+
};
|
|
350
375
|
/**
|
|
351
376
|
* Reports the HQ lock for status output without acquiring or changing it.
|
|
352
377
|
* @param root The trusted state root.
|
|
@@ -210,7 +210,7 @@ export const createClaudeProvider = (store) => {
|
|
|
210
210
|
* - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
|
|
211
211
|
*/
|
|
212
212
|
async readConfiguration(command, signal) {
|
|
213
|
-
return withReadConnection(command.workspaceRoot, signal, async (native) => toClaudeConfiguration(await native.requestControl({ subtype: 'get_settings' }), native.initialization.agents, native.initialization.current_permission_mode, undefined, {}, command));
|
|
213
|
+
return withReadConnection(command.workspaceRoot, signal, async (native) => toClaudeConfiguration(await native.requestControl({ subtype: 'get_settings' }), native.initialization.agents, native.initialization.current_permission_mode, undefined, false, {}, command));
|
|
214
214
|
},
|
|
215
215
|
/**
|
|
216
216
|
* Reads fresh native account metadata in a disposable read lane.
|
|
@@ -8,9 +8,11 @@ import type { IConfigurationRequest, ISessionConfiguration, ISessionSettings } f
|
|
|
8
8
|
export declare const toClaudeSessionFlags: (settings: ISessionSettings) => string[];
|
|
9
9
|
/**
|
|
10
10
|
* Reads known native settings without exposing sources, hooks, tokens or arbitrary configuration.
|
|
11
|
+
* Instructions are reported only for a process that started its conversation: Claude records them
|
|
12
|
+
* once per conversation (`--system-prompt-snapshot`, on by default) and reuses them on resume.
|
|
11
13
|
* @returns Effective session settings and a bounded, query-bound native agent page.
|
|
12
14
|
* @throws
|
|
13
15
|
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
14
16
|
* - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
|
|
15
17
|
*/
|
|
16
|
-
export declare const toClaudeConfiguration: (input: unknown, agentsInput: unknown, permissionMode: unknown, nativeId: string | undefined, settings: ISessionSettings, command: IConfigurationRequest) => ISessionConfiguration;
|
|
18
|
+
export declare const toClaudeConfiguration: (input: unknown, agentsInput: unknown, permissionMode: unknown, nativeId: string | undefined, isResumed: boolean, settings: ISessionSettings, command: IConfigurationRequest) => ISessionConfiguration;
|
|
@@ -44,12 +44,14 @@ export const toClaudeSessionFlags = (settings) => {
|
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
46
|
* Reads known native settings without exposing sources, hooks, tokens or arbitrary configuration.
|
|
47
|
+
* Instructions are reported only for a process that started its conversation: Claude records them
|
|
48
|
+
* once per conversation (`--system-prompt-snapshot`, on by default) and reuses them on resume.
|
|
47
49
|
* @returns Effective session settings and a bounded, query-bound native agent page.
|
|
48
50
|
* @throws
|
|
49
51
|
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
50
52
|
* - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
|
|
51
53
|
*/
|
|
52
|
-
export const toClaudeConfiguration = (input, agentsInput, permissionMode, nativeId, settings, command) => {
|
|
54
|
+
export const toClaudeConfiguration = (input, agentsInput, permissionMode, nativeId, isResumed, settings, command) => {
|
|
53
55
|
const parsed = SettingsSchema.safeParse(input);
|
|
54
56
|
const agents = AgentsSchema.safeParse(agentsInput);
|
|
55
57
|
if (!parsed.success)
|
|
@@ -82,8 +84,8 @@ export const toClaudeConfiguration = (input, agentsInput, permissionMode, native
|
|
|
82
84
|
nativeProfile: null,
|
|
83
85
|
permissionMode: typeof permissionMode === 'string' ? permissionMode : null,
|
|
84
86
|
sandbox: null,
|
|
85
|
-
systemInstructions: nativeId ? (settings.systemInstructions ?? null) : null,
|
|
86
|
-
developerInstructions: nativeId ? (settings.developerInstructions ?? null) : null,
|
|
87
|
+
systemInstructions: nativeId && !isResumed ? (settings.systemInstructions ?? null) : null,
|
|
88
|
+
developerInstructions: nativeId && !isResumed ? (settings.developerInstructions ?? null) : null,
|
|
87
89
|
},
|
|
88
90
|
instructionSources: { data: [], isComplete: false },
|
|
89
91
|
choices: agents.success ? page.choices : null,
|
|
@@ -217,7 +217,9 @@ export declare const createClaudeControl: (additionalDirectories?: string[]) =>
|
|
|
217
217
|
nameThread(requestId: string, nativeId: string, name: string): Promise<void>;
|
|
218
218
|
validateInput: (nativeId: string, settings: ITurnSettings) => void;
|
|
219
219
|
/**
|
|
220
|
-
* Writes
|
|
220
|
+
* Writes input once and waits for its matching native echo or result. Input written while a
|
|
221
|
+
* turn Claude started runs is replayed only after that turn's result, so a long turn can
|
|
222
|
+
* outlast the wait, and the late replay reconciles it.
|
|
221
223
|
* @returns The native user-message identity after acknowledgement.
|
|
222
224
|
* @throws
|
|
223
225
|
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
@@ -229,7 +231,8 @@ export declare const createClaudeControl: (additionalDirectories?: string[]) =>
|
|
|
229
231
|
id: string;
|
|
230
232
|
}>;
|
|
231
233
|
/**
|
|
232
|
-
* Requests interruption of the exact active input; completion
|
|
234
|
+
* Requests interruption of the exact active turn, HQ input's or one Claude started; completion
|
|
235
|
+
* arrives separately.
|
|
233
236
|
* @returns Acknowledgement of the native interruption request.
|
|
234
237
|
* @throws
|
|
235
238
|
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
@@ -51,6 +51,8 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
51
51
|
session.idleStop?.abort();
|
|
52
52
|
session.idleStop = undefined;
|
|
53
53
|
};
|
|
54
|
+
// a session is busy while HQ input waits or runs, or while a turn Claude started runs
|
|
55
|
+
const isBusy = (session) => session.input !== undefined || session.nativeTurn !== undefined;
|
|
54
56
|
/** Parks the session once it stays idle for the whole period, restarting any earlier wait. */
|
|
55
57
|
const scheduleIdleStop = (session) => {
|
|
56
58
|
cancelIdleStop(session);
|
|
@@ -62,17 +64,22 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
62
64
|
};
|
|
63
65
|
/**
|
|
64
66
|
* Reads an idle session's native settings once, then closes its process. The session stays idle
|
|
65
|
-
* and records nothing; a call that arrives first keeps the process
|
|
67
|
+
* and records nothing; a call that arrives first keeps the process, and a background command
|
|
68
|
+
* still running holds the session for another idle period, since closing would end it.
|
|
66
69
|
*/
|
|
67
70
|
const park = async (session) => {
|
|
68
71
|
const connection = session.connection;
|
|
69
72
|
if (isClosing ||
|
|
70
73
|
session.isClosing ||
|
|
71
74
|
session.input ||
|
|
75
|
+
session.nativeTurn ||
|
|
72
76
|
session.transition ||
|
|
73
77
|
!connection?.isAvailable)
|
|
74
78
|
return;
|
|
75
|
-
if (activeCalls.has(session.id) ||
|
|
79
|
+
if (activeCalls.has(session.id) ||
|
|
80
|
+
session.requests.size ||
|
|
81
|
+
session.backgroundTaskCount ||
|
|
82
|
+
hasExtensionRequest(session.id)) {
|
|
76
83
|
scheduleIdleStop(session);
|
|
77
84
|
return;
|
|
78
85
|
}
|
|
@@ -212,6 +219,10 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
212
219
|
*/
|
|
213
220
|
const start = async (session, isResume, forkSourceNativeId, signal) => {
|
|
214
221
|
const nativeId = session.id;
|
|
222
|
+
session.isResumed = isResume || forkSourceNativeId !== undefined;
|
|
223
|
+
// a new process has no turn of its own and no background commands yet
|
|
224
|
+
session.nativeTurn = undefined;
|
|
225
|
+
session.backgroundTaskCount = 0;
|
|
215
226
|
const connection = await createClaudeRpc((reason) => {
|
|
216
227
|
if (reason !== 'unavailable' || session.isClosing || session.parked)
|
|
217
228
|
return;
|
|
@@ -233,10 +244,12 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
233
244
|
settings: session.settings,
|
|
234
245
|
...(forkSourceNativeId ? { forkSourceNativeId } : {}),
|
|
235
246
|
receive: (raw) => {
|
|
236
|
-
const
|
|
247
|
+
const wasBusy = isBusy(session);
|
|
237
248
|
receiveClaudeMessage(session, raw, events);
|
|
238
|
-
// the end of
|
|
239
|
-
if (
|
|
249
|
+
// a turn Claude starts holds the idle period back, and the end of the last turn starts it
|
|
250
|
+
if (!wasBusy && isBusy(session))
|
|
251
|
+
cancelIdleStop(session);
|
|
252
|
+
else if (wasBusy && !isBusy(session))
|
|
240
253
|
scheduleIdleStop(session);
|
|
241
254
|
},
|
|
242
255
|
lateResponse(id, result, error) {
|
|
@@ -314,6 +327,8 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
314
327
|
scope: randomUUID(),
|
|
315
328
|
isClosing: false,
|
|
316
329
|
hasInput: isResume || forkSourceNativeId !== undefined,
|
|
330
|
+
isResumed: false,
|
|
331
|
+
backgroundTaskCount: 0,
|
|
317
332
|
opening: Promise.resolve(),
|
|
318
333
|
previews: new Map(),
|
|
319
334
|
requests: new Map(),
|
|
@@ -339,7 +354,7 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
339
354
|
if (command.kind === 'mcp-login' || command.kind === 'mcp-reload')
|
|
340
355
|
capabilityUnavailable();
|
|
341
356
|
return withSession(nativeId, true, async (session) => {
|
|
342
|
-
if (session
|
|
357
|
+
if (isBusy(session) || session.requests.size || extensionRequests.size >= 32)
|
|
343
358
|
conflict();
|
|
344
359
|
extensionRequests.set(requestId, { nativeId, kind: command.kind });
|
|
345
360
|
const body = command.kind === 'skills-reload'
|
|
@@ -493,7 +508,7 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
493
508
|
agents: session.connection.initialization.agents,
|
|
494
509
|
permissionMode: session.connection.initialization.current_permission_mode,
|
|
495
510
|
};
|
|
496
|
-
return toClaudeConfiguration(native.configuration, native.agents, native.permissionMode, nativeId, session.settings, command);
|
|
511
|
+
return toClaudeConfiguration(native.configuration, native.agents, native.permissionMode, nativeId, session.isResumed, session.settings, command);
|
|
497
512
|
});
|
|
498
513
|
},
|
|
499
514
|
/**
|
|
@@ -510,7 +525,9 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
510
525
|
},
|
|
511
526
|
validateInput,
|
|
512
527
|
/**
|
|
513
|
-
* Writes
|
|
528
|
+
* Writes input once and waits for its matching native echo or result. Input written while a
|
|
529
|
+
* turn Claude started runs is replayed only after that turn's result, so a long turn can
|
|
530
|
+
* outlast the wait, and the late replay reconciles it.
|
|
514
531
|
* @returns The native user-message identity after acknowledgement.
|
|
515
532
|
* @throws
|
|
516
533
|
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
@@ -597,7 +614,8 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
597
614
|
}, signal);
|
|
598
615
|
},
|
|
599
616
|
/**
|
|
600
|
-
* Requests interruption of the exact active input; completion
|
|
617
|
+
* Requests interruption of the exact active turn, HQ input's or one Claude started; completion
|
|
618
|
+
* arrives separately.
|
|
601
619
|
* @returns Acknowledgement of the native interruption request.
|
|
602
620
|
* @throws
|
|
603
621
|
* - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
|
|
@@ -606,9 +624,10 @@ export const createClaudeControl = (additionalDirectories = []) => {
|
|
|
606
624
|
async interrupt(requestId, nativeId, turnId) {
|
|
607
625
|
// a parked session is idle, so its interrupt is refused as any idle session's is
|
|
608
626
|
await withSession(nativeId, false, async (session) => {
|
|
609
|
-
|
|
627
|
+
const turn = session.nativeTurn ?? session.input;
|
|
628
|
+
if (turn?.id !== turnId)
|
|
610
629
|
conflict();
|
|
611
|
-
|
|
630
|
+
turn.interruptRequested = true;
|
|
612
631
|
await session.connection.requestControl({ subtype: 'interrupt' }, requestId);
|
|
613
632
|
});
|
|
614
633
|
},
|
|
@@ -11,7 +11,11 @@ const QuestionSchema = z.object({
|
|
|
11
11
|
.min(1)
|
|
12
12
|
.max(32),
|
|
13
13
|
});
|
|
14
|
-
|
|
14
|
+
/*
|
|
15
|
+
* Claude adds fields to its permission requests between releases, so the known fields are
|
|
16
|
+
* validated and the rest are dropped; the reply only ever echoes the request's own input.
|
|
17
|
+
*/
|
|
18
|
+
const RequestSchema = z.object({
|
|
15
19
|
subtype: z.literal('can_use_tool'),
|
|
16
20
|
tool_name: z.string().min(1).max(256),
|
|
17
21
|
tool_use_id: z.string().min(1).max(4096),
|
|
@@ -49,7 +53,7 @@ const RequestSchema = z.strictObject({
|
|
|
49
53
|
description: z.string().max(8192).optional(),
|
|
50
54
|
requires_user_interaction: z.boolean().optional(),
|
|
51
55
|
});
|
|
52
|
-
const ElicitationRequestSchema = z.
|
|
56
|
+
const ElicitationRequestSchema = z.object({
|
|
53
57
|
subtype: z.literal('elicitation'),
|
|
54
58
|
mcp_server_name: z.string().min(1).max(256),
|
|
55
59
|
message: z.string().max(8192),
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { IClaudeControlEvents, IClaudeSession } from './types.js';
|
|
2
2
|
/**
|
|
3
|
-
* Normalizes native stream evidence without retaining another conversation buffer.
|
|
3
|
+
* Normalizes native stream evidence without retaining another conversation buffer. A turn frame
|
|
4
|
+
* that arrives while no turn is active, and isn't the waiting input's replay, starts a turn of
|
|
5
|
+
* Claude's own; frames then belong to the active turn until its result.
|
|
4
6
|
* @throws
|
|
5
7
|
* - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
|
|
6
8
|
*/
|