jg-agent-hq 4.0.2 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +4 -4
  3. package/api/README.md +20 -12
  4. package/dist/cli/cli.js +23 -2
  5. package/dist/contracts/agent-tools/types.d.ts +2 -0
  6. package/dist/contracts/browser/types.d.ts +11 -0
  7. package/dist/contracts/constants.d.ts +1 -0
  8. package/dist/contracts/constants.js +2 -0
  9. package/dist/contracts/evidence/types.d.ts +4 -0
  10. package/dist/contracts/evidence/types.js +2 -1
  11. package/dist/contracts/index.d.ts +5 -5
  12. package/dist/contracts/index.js +3 -3
  13. package/dist/contracts/sessions/index.d.ts +2 -2
  14. package/dist/contracts/sessions/index.js +1 -1
  15. package/dist/contracts/sessions/types.d.ts +5 -0
  16. package/dist/contracts/sessions/types.js +2 -0
  17. package/dist/contracts/types.d.ts +242 -0
  18. package/dist/contracts/types.js +37 -5
  19. package/dist/providers/claude/agents/agents.d.ts +7 -3
  20. package/dist/providers/claude/agents/agents.js +33 -19
  21. package/dist/providers/claude/agents/constants.d.ts +2 -1
  22. package/dist/providers/claude/agents/constants.js +3 -2
  23. package/dist/providers/claude/claude.js +80 -30
  24. package/dist/providers/claude/control/control.d.ts +44 -5
  25. package/dist/providers/claude/control/control.js +425 -228
  26. package/dist/providers/claude/control/stream.js +22 -6
  27. package/dist/providers/claude/control/types.d.ts +7 -0
  28. package/dist/providers/claude/history/history.model.js +44 -27
  29. package/dist/providers/claude/history/types.d.ts +88 -0
  30. package/dist/providers/claude/history/types.js +15 -0
  31. package/dist/providers/claude/history/utilities.d.ts +6 -4
  32. package/dist/providers/claude/history/utilities.js +146 -52
  33. package/dist/providers/codex/adapter.js +33 -4
  34. package/dist/providers/codex/codex.js +5 -4
  35. package/dist/providers/codex/events.js +3 -1
  36. package/dist/providers/codex/transformers.d.ts +4 -2
  37. package/dist/providers/codex/transformers.js +80 -21
  38. package/dist/providers/codex/types.d.ts +4 -3
  39. package/dist/providers/registry/registry.js +12 -9
  40. package/dist/providers/registry/types.d.ts +1 -1
  41. package/dist/providers/registry/validations.js +7 -0
  42. package/dist/providers/types.d.ts +7 -1
  43. package/dist/runtime/runtime.js +20 -4
  44. package/dist/sessions/evidence/utilities.js +3 -1
  45. package/dist/sessions/index.d.ts +2 -2
  46. package/dist/sessions/index.js +3 -3
  47. package/dist/sessions/sessions.d.ts +11 -1
  48. package/dist/sessions/sessions.js +89 -7
  49. package/dist/sessions/utilities.d.ts +15 -0
  50. package/dist/sessions/utilities.js +19 -0
  51. package/dist/store/schema.sql +1 -1
  52. package/dist/store/sessions/index.d.ts +2 -0
  53. package/dist/store/sessions/index.js +2 -0
  54. package/dist/store/sessions/sessions.model.d.ts +9 -0
  55. package/dist/store/sessions/sessions.model.js +55 -0
  56. package/dist/store/sessions/types.d.ts +67 -0
  57. package/dist/store/sessions/types.js +36 -0
  58. package/dist/store/store.js +4 -0
  59. package/dist/store/store.worker.js +3 -0
  60. package/dist/store/types.d.ts +40 -0
  61. package/dist/store/types.js +1 -0
  62. package/package.json +1 -1
@@ -1,4 +1,5 @@
1
1
  import { Exception, isException } from 'jg-exception';
2
+ import { delay } from 'jg-utilities';
2
3
  import { scheduler } from 'node:timers/promises';
3
4
  import { z } from 'zod';
4
5
  import { createClaudeRpc } from './rpc.js';
@@ -7,6 +8,8 @@ import { toClaudeUsageSnapshot } from './transformers.js';
7
8
  import { readClaudeModels } from './models/index.js';
8
9
  import { toClaudeConfiguration } from './configuration/index.js';
9
10
  import { toClaudeAccount } from './accounts/index.js';
11
+ // the session-less read process closes after this long without a read, and reopens on the next one
12
+ const METADATA_IDLE_SECONDS = 300;
10
13
  /**
11
14
  * Lazily connects read-only Claude capabilities; Codex needs no Claude installation.
12
15
  * @returns The read adapter with independent usage and transcript availability.
@@ -17,6 +20,11 @@ export const createClaudeProvider = (store) => {
17
20
  const metadataReads = new Set();
18
21
  let isClosing = false;
19
22
  let generation = 0;
23
+ let reads = 0;
24
+ let idleClose;
25
+ // a closing idle read process, whose failure surfaces at shutdown
26
+ let retiring;
27
+ let version = null;
20
28
  let refresh;
21
29
  const unavailable = () => ({
22
30
  provider: 'claude',
@@ -38,6 +46,7 @@ export const createClaudeProvider = (store) => {
38
46
  else
39
47
  latest = { ...latest, state: latest.observedAt === null ? 'unavailable' : 'stale' };
40
48
  });
49
+ version = connection.version;
41
50
  return connection;
42
51
  };
43
52
  const open = () => {
@@ -48,6 +57,40 @@ export const createClaudeProvider = (store) => {
48
57
  }));
49
58
  return (opening ??= connect());
50
59
  };
60
+ /** Closes the read process once it has gone unused, so the next read opens a fresh one. */
61
+ const retire = () => {
62
+ idleClose = undefined;
63
+ const native = connection;
64
+ if (!native || reads > 0 || isClosing)
65
+ return;
66
+ connection = undefined;
67
+ opening = undefined;
68
+ retiring = native.close();
69
+ retiring.catch(() => undefined);
70
+ };
71
+ /**
72
+ * Runs one read on the session-less process, opening it when it is closed.
73
+ * @returns The read's result; the process closes after the idle period without another read.
74
+ * @throws
75
+ * - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
76
+ * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
77
+ */
78
+ const withMetadata = async (read) => {
79
+ idleClose?.abort();
80
+ idleClose = undefined;
81
+ reads += 1;
82
+ try {
83
+ return await read(await open());
84
+ }
85
+ finally {
86
+ reads -= 1;
87
+ if (reads === 0 && !isClosing) {
88
+ const controller = new AbortController();
89
+ idleClose = controller;
90
+ void delay(METADATA_IDLE_SECONDS, { signal: controller.signal }).then(retire, () => undefined);
91
+ }
92
+ }
93
+ };
51
94
  /** Runs one bounded metadata read and confirms native shutdown before releasing its slot. */
52
95
  const withReadConnection = async (workspaceRoot, signal, read) => {
53
96
  if (isClosing || metadataReads.size >= 4)
@@ -91,6 +134,8 @@ export const createClaudeProvider = (store) => {
91
134
  * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
92
135
  */
93
136
  async invalidateAuthentication() {
137
+ idleClose?.abort();
138
+ idleClose = undefined;
94
139
  generation += 1;
95
140
  latest = unavailable();
96
141
  if (opening) {
@@ -108,7 +153,7 @@ export const createClaudeProvider = (store) => {
108
153
  * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
109
154
  */
110
155
  async ensureReady() {
111
- await open();
156
+ await withMetadata(async () => undefined);
112
157
  },
113
158
  /**
114
159
  * Reads current native model choices without a user prompt.
@@ -119,8 +164,7 @@ export const createClaudeProvider = (store) => {
119
164
  * - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
120
165
  */
121
166
  async readModels(command) {
122
- const native = await open();
123
- return readClaudeModels(await native.requestControl({ subtype: 'list_models' }), command);
167
+ return withMetadata(async (native) => readClaudeModels(await native.requestControl({ subtype: 'list_models' }), command));
124
168
  },
125
169
  /**
126
170
  * Opens a workspace-scoped read lane without a user prompt or model turn.
@@ -143,8 +187,9 @@ export const createClaudeProvider = (store) => {
143
187
  async readAccount(signal) {
144
188
  return withReadConnection(undefined, signal, async (native) => toClaudeAccount(native.initialization.account, native.initializedAt));
145
189
  },
190
+ // the last observed version stays known while the read process is closed
146
191
  get version() {
147
- return connection?.version ?? null;
192
+ return connection?.version ?? version;
148
193
  },
149
194
  get isAvailable() {
150
195
  return connection?.isAvailable ?? false;
@@ -164,7 +209,7 @@ export const createClaudeProvider = (store) => {
164
209
  */
165
210
  async readHistory(command) {
166
211
  const deadline = Date.now() + 30_000;
167
- await open();
212
+ await withMetadata(async () => undefined);
168
213
  while (!isClosing && Date.now() < deadline) {
169
214
  const result = await store.readClaudeHistory(command);
170
215
  if (result.kind !== 'indexing')
@@ -187,18 +232,18 @@ export const createClaudeProvider = (store) => {
187
232
  return refresh;
188
233
  return (refresh = (async () => {
189
234
  try {
190
- const native = await open();
191
- const epoch = generation;
192
- const parsed = ClaudeUsageSchema.safeParse(await native.requestUsage());
193
- if (!parsed.success)
194
- throw new Exception('The Claude integration failed', {
195
- code: 'JG_AGENT_HQ_CLAUDE_FAILED',
196
- data: { kind: 'agent-unavailable' },
197
- cause: parsed.error,
198
- });
199
- if (epoch !== generation)
200
- return latest;
201
- latest = toClaudeUsageSnapshot(parsed.data, Date.now());
235
+ await withMetadata(async (native) => {
236
+ const epoch = generation;
237
+ const parsed = ClaudeUsageSchema.safeParse(await native.requestUsage());
238
+ if (!parsed.success)
239
+ throw new Exception('The Claude integration failed', {
240
+ code: 'JG_AGENT_HQ_CLAUDE_FAILED',
241
+ data: { kind: 'agent-unavailable' },
242
+ cause: parsed.error,
243
+ });
244
+ if (epoch === generation)
245
+ latest = toClaudeUsageSnapshot(parsed.data, Date.now());
246
+ });
202
247
  }
203
248
  catch {
204
249
  latest = { ...latest, state: latest.observedAt === null ? 'unavailable' : 'stale' };
@@ -221,19 +266,24 @@ export const createClaudeProvider = (store) => {
221
266
  */
222
267
  async close() {
223
268
  isClosing = true;
224
- const results = await Promise.allSettled([...(opening ? [opening] : []), ...metadataReads].map(async (starting) => {
225
- let native;
226
- try {
227
- native = await starting;
228
- }
229
- catch (cause) {
230
- if (isException(cause) &&
231
- z.object({ cleanupUnconfirmed: z.literal(true) }).safeParse(cause.data).success)
232
- throw cause;
233
- return;
234
- }
235
- await native.close();
236
- }));
269
+ idleClose?.abort();
270
+ idleClose = undefined;
271
+ const results = await Promise.allSettled([
272
+ ...(retiring ? [retiring] : []),
273
+ ...[...(opening ? [opening] : []), ...metadataReads].map(async (starting) => {
274
+ let native;
275
+ try {
276
+ native = await starting;
277
+ }
278
+ catch (cause) {
279
+ if (isException(cause) &&
280
+ z.object({ cleanupUnconfirmed: z.literal(true) }).safeParse(cause.data).success)
281
+ throw cause;
282
+ return;
283
+ }
284
+ await native.close();
285
+ }),
286
+ ]);
237
287
  const failed = results.find((result) => result.status === 'rejected');
238
288
  if (failed?.status === 'rejected')
239
289
  throw failed.reason;
@@ -9,16 +9,30 @@ import type { IClaudeControlEvents } from './types.js';
9
9
  */
10
10
  export declare const createClaudeControl: (additionalDirectories?: string[], agentTools?: IProviderAgentTools) => {
11
11
  /**
12
- * Applies one supported native extension command to an idle owned session.
12
+ * Applies one supported native extension command to an idle owned session, reopening a parked one.
13
13
  * @returns Native acknowledgement, preserving held plugin reloads explicitly.
14
14
  * @throws
15
15
  * - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
16
+ * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
17
+ * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
16
18
  * - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
17
19
  * - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
20
+ * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
18
21
  */
19
22
  controlExtensions(requestId: string, nativeId: string, workspaceRoot: string, command: IExtensionControlCommand): Promise<INativeControlResult>;
20
23
  readonly isAvailable: boolean;
21
- /** @returns Native MCP status for this exact controlled session. */
24
+ /**
25
+ * Reads native MCP status for this exact controlled session, reopening a parked one.
26
+ * @returns The native server page.
27
+ * @throws
28
+ * - JG_AGENT_HQ_CURSOR_INVALID: The HQ cursor does not match this request
29
+ * - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
30
+ * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
31
+ * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
32
+ * - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
33
+ * - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
34
+ * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
35
+ */
22
36
  readMcpServers(nativeId: string, limit: number, cursor?: string): Promise<{
23
37
  state: "unsupported" | "unavailable" | "available" | "partial";
24
38
  observedAt: number | null;
@@ -35,7 +49,17 @@ export declare const createClaudeControl: (additionalDirectories?: string[], age
35
49
  }[];
36
50
  nextCursor: string | null;
37
51
  }>;
38
- /** @returns Native tool metadata for one configured session server. */
52
+ /**
53
+ * Reads native tool metadata for one configured session server, reopening a parked one.
54
+ * @returns The server's native tool metadata.
55
+ * @throws
56
+ * - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
57
+ * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
58
+ * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
59
+ * - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
60
+ * - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
61
+ * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
62
+ */
39
63
  readMcpTools(nativeId: string, serverName: string): Promise<{
40
64
  state: "unsupported" | "unavailable" | "available" | "partial";
41
65
  observedAt: number | null;
@@ -50,7 +74,17 @@ export declare const createClaudeControl: (additionalDirectories?: string[], age
50
74
  isOpenWorld: boolean | null;
51
75
  }[];
52
76
  }>;
53
- /** @returns An explicit unsupported native resource catalog observation. */
77
+ /**
78
+ * Reports the native resource catalog, reopening a parked session.
79
+ * @returns An explicit unsupported native resource catalog observation.
80
+ * @throws
81
+ * - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
82
+ * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
83
+ * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
84
+ * - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
85
+ * - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
86
+ * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
87
+ */
54
88
  readMcpResources(nativeId: string): Promise<{
55
89
  state: "unsupported" | "unavailable" | "available" | "partial";
56
90
  observedAt: number | null;
@@ -186,10 +220,15 @@ export declare const createClaudeControl: (additionalDirectories?: string[], age
186
220
  source: "native";
187
221
  }>;
188
222
  /**
189
- * Names only the native session selected by the admitted action.
223
+ * Names only the native session selected by the admitted action, reopening a parked one.
190
224
  * @returns Native acknowledgement of the name change.
191
225
  * @throws
226
+ * - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
227
+ * - JG_AGENT_HQ_COLLABORATION_FORBIDDEN: The HQ collaboration operation is not authorized
228
+ * - JG_AGENT_HQ_COLLABORATION_CONFLICT: The HQ collaboration operation conflicts with its recorded state
229
+ * - JG_AGENT_HQ_CAPABILITY_UNAVAILABLE: The HQ capability is unavailable
192
230
  * - JG_AGENT_HQ_CLAUDE_FAILED: The Claude integration failed
231
+ * - JG_AGENT_HQ_TRANSPORT_FAILED: The HQ transport failed
193
232
  */
194
233
  nameThread(requestId: string, nativeId: string, name: string): Promise<void>;
195
234
  validateInput: (nativeId: string, settings: ITurnSettings) => void;