jg-agent-hq 10.5.2 → 10.5.4

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 10.5.4
4
+
5
+ - Keep a Claude response's parallel tool calls in its history when their results finish out of order. Claude Code links each result to its own call, so a result that came back after a later call's result started a side branch, and history left out the later calls and results: they stayed `awaiting-flush` after the session's answer, and an open page's cursor was rejected as changed. Such a result now follows the latest record of its turn. Every Claude history is indexed again once.
6
+
7
+ ## 10.5.3
8
+
9
+ - Leave a Claude subagent's stream frames, which name the Agent call that started it, out of a session's live items and turns. History leaves the subagent's sidechain out, so its prompt, tool calls and results stayed `awaiting-flush` after the session's answer, its prompt read as the operator's message, and a background subagent's frames after the session's turn started a turn of Claude's own that never ended, which held the next message as starting. A subagent's tool results still settle the approvals it asked for.
10
+
3
11
  ## 10.5.2
4
12
 
5
13
  - Show a Claude tool result made of content blocks, such as a browser tool's caption and screenshot, as its text, with each image as `[Image]`. Such results used to read `[Native tool_result]`.
package/api/README.md CHANGED
@@ -241,7 +241,7 @@ The existing SQLite worker incrementally records file identity, complete-line of
241
241
 
242
242
  Current-chain membership and turn starts are indexed as metadata. Ordinary item and turn continuations seek those indexes; they do not walk the full chain per page. A changed branch rebuilds membership before exposing that revision. Repeated native message identities can replace content while retaining their parent; a changed parent for the same identity is an unsupported format.
243
243
 
244
- Selected history reads refresh only that file, retain incomplete tails, and detect truncation or replaced identity. Changed branch/file revisions invalidate item and turn continuations. Only the current parent-linked conversation chain is returned. Text is reread from the native file for the selected page and normalized as plain text. Thinking blocks are left out, and a record holding only thinking is hidden, as are `<task-notification>` records. Compaction summaries, `<local-command-stdout>` output and interruption markers are `notice` items, and a slash command is a `user-message` reading `/name args`. A `!` shell input and its output are `command` items, the output's `callId` being the input's UUID. A tool call shows its native name and input, with the block's ID as `callId`, which is null for a record calling several tools; its result is a `tool` item carrying that ID, `failed` when it has `is_error` and `succeeded` otherwise. An Edit or Write result whose metadata has a `filePath` and a `structuredPatch` is a `file-change` item reading the path and its patch lines, or a created file's content, every line of which counts as added. A result Claude Code saved to a file reads `Output too large (<size>), saved to a file` followed by its preview, without the file's path. A result made of content blocks, such as a screenshot with its caption, reads as its text blocks, each image as `[Image]`. Other non-text content has explicit native block labels. The index stores each entry's presented kind and visibility, so evidence kind filters match `items` and hidden records never shorten a page. An unknown record containing content is returned as an `unsupported` item when its identity and chain are trustworthy; it cannot imply an agent response, idle state or completion. Each selected page reads at most 4 MiB of record bytes beyond its first record and returns at most 4 MiB of serialized item presentation, so a large-text page can contain fewer rows than its requested limit.
244
+ Selected history reads refresh only that file, retain incomplete tails, and detect truncation or replaced identity. Changed branch/file revisions invalidate item and turn continuations. Only the current parent-linked conversation chain is returned. Claude Code links each tool result to its own call, so parallel calls whose results finish out of order would leave the later calls and results on a side branch; such a result follows the latest record of its call's turn instead, keeping every call of the response and all their results on the chain without changing its revision. Text is reread from the native file for the selected page and normalized as plain text. Thinking blocks are left out, and a record holding only thinking is hidden, as are `<task-notification>` records and a subagent's sidechain records. Live items leave a subagent's stream frames out likewise, and those frames start no turn, though a subagent's tool results still settle the approvals it asked for. Compaction summaries, `<local-command-stdout>` output and interruption markers are `notice` items, and a slash command is a `user-message` reading `/name args`. A `!` shell input and its output are `command` items, the output's `callId` being the input's UUID. A tool call shows its native name and input, with the block's ID as `callId`, which is null for a record calling several tools; its result is a `tool` item carrying that ID, `failed` when it has `is_error` and `succeeded` otherwise. An Edit or Write result whose metadata has a `filePath` and a `structuredPatch` is a `file-change` item reading the path and its patch lines, or a created file's content, every line of which counts as added. A result Claude Code saved to a file reads `Output too large (<size>), saved to a file` followed by its preview, without the file's path. A result made of content blocks, such as a screenshot with its caption, reads as its text blocks, each image as `[Image]`. Other non-text content has explicit native block labels. The index stores each entry's presented kind and visibility, so evidence kind filters match `items` and hidden records never shorten a page. An unknown record containing content is returned as an `unsupported` item when its identity and chain are trustworthy; it cannot imply an agent response, idle state or completion. Each selected page reads at most 4 MiB of record bytes beyond its first record and returns at most 4 MiB of serialized item presentation, so a large-text page can contain fewer rows than its requested limit.
245
245
 
246
246
  File timestamps and records do not prove that a native process is idle or a turn completed. Claude file-backed thread and turn states are therefore `unknown`, with no invented completion timestamp. Explicit native custom titles are preserved; a missing title remains null. A turn starts at a message the operator sends, including a slash command; task notifications, compaction summaries, local command output, interruption markers and `!` shell records belong to the turn before them. Grouping follows parent links and is not an authoritative execution boundary. A compaction starts a new chain whose first record names the record before it, so history reads on past the compaction to the start of the conversation.
247
247
 
@@ -5,7 +5,12 @@ import { RecordSchema, toClaudeItem } from '../history/index.js';
5
5
  import { MAX_RESULT_BYTES } from '../../../contracts/index.js';
6
6
  import { toTurnResult } from '../../results/index.js';
7
7
  import { createClaudePrompt } from './permissions.js';
8
- const FrameSchema = z.object({ type: z.string(), session_id: z.string().optional() });
8
+ const FrameSchema = z.object({
9
+ type: z.string(),
10
+ session_id: z.string().optional(),
11
+ // the Agent call whose subagent a frame comes from, which history keeps out of the session
12
+ parent_tool_use_id: z.string().nullish(),
13
+ });
9
14
  const MessageSchema = z.object({
10
15
  uuid: z.string().min(1).max(4096),
11
16
  message: z.object({ role: z.string(), content: z.unknown() }),
@@ -135,6 +140,26 @@ const endTurn = (session, events) => {
135
140
  session.previews.clear();
136
141
  session.requests.clear();
137
142
  };
143
+ /** Settles the approvals a user frame's tool results answer, as Claude ran or refused each tool. */
144
+ const resolveRequests = (session, message, events) => {
145
+ const results = ToolResultsSchema.safeParse(message.message.content);
146
+ if (!results.success)
147
+ return;
148
+ for (const result of results.data) {
149
+ if (result.type !== 'tool_result' || !result.tool_use_id)
150
+ continue;
151
+ for (const [id, request] of session.requests)
152
+ if (request.toolId === result.tool_use_id) {
153
+ events?.observe({
154
+ kind: 'request-resolved',
155
+ provider: 'claude',
156
+ nativeThreadId: session.id,
157
+ requestId: request.scopedId,
158
+ });
159
+ session.requests.delete(id);
160
+ }
161
+ }
162
+ };
138
163
  /**
139
164
  * Normalizes native stream evidence without retaining another conversation buffer. A turn frame
140
165
  * that arrives while no turn is active, and isn't the waiting input's replay, starts a turn of
@@ -146,6 +171,17 @@ export const receiveClaudeMessage = (session, raw, events) => {
146
171
  const frame = FrameSchema.parse(raw);
147
172
  if (frame.session_id && frame.session_id !== session.id)
148
173
  unavailable();
174
+ /*
175
+ * A subagent's frames name the Agent call that started it. History leaves its sidechain out, so
176
+ * they show no live item, start no turn and hold no result, even after the session's turn ended
177
+ * while a background subagent runs on; its tool results still settle the approvals it asked for.
178
+ */
179
+ if (frame.parent_tool_use_id &&
180
+ (frame.type === 'user' || frame.type === 'assistant' || frame.type === 'stream_event')) {
181
+ if (frame.type === 'user')
182
+ resolveRequests(session, MessageSchema.parse(raw), events);
183
+ return;
184
+ }
149
185
  if (frame.type === 'user' || frame.type === 'assistant') {
150
186
  const message = MessageSchema.parse(raw);
151
187
  const isReplay = frame.type === 'user' && session.input !== undefined && message.uuid === session.input.id;
@@ -208,24 +244,8 @@ export const receiveClaudeMessage = (session, raw, events) => {
208
244
  }
209
245
  if (item)
210
246
  events?.item(session.id, item, true, replacedIds);
211
- if (frame.type === 'user') {
212
- const results = ToolResultsSchema.safeParse(message.message.content);
213
- if (results.success)
214
- for (const result of results.data) {
215
- if (result.type !== 'tool_result' || !result.tool_use_id)
216
- continue;
217
- for (const [id, request] of session.requests)
218
- if (request.toolId === result.tool_use_id) {
219
- events?.observe({
220
- kind: 'request-resolved',
221
- provider: 'claude',
222
- nativeThreadId: session.id,
223
- requestId: request.scopedId,
224
- });
225
- session.requests.delete(id);
226
- }
227
- }
228
- }
247
+ if (frame.type === 'user')
248
+ resolveRequests(session, message, events);
229
249
  }
230
250
  else if (frame.type === 'stream_event') {
231
251
  const turn = getActiveTurn(session) ?? startNativeTurn(session, events);
@@ -63,6 +63,15 @@ export declare class ClaudeHistoryModel {
63
63
  * - JG_AGENT_HQ_CLAUDE_HISTORY_UNAVAILABLE: The Claude history is unavailable
64
64
  */
65
65
  private __record;
66
+ /**
67
+ * Claude Code links a tool result to its own call. When parallel calls finish out of order, an
68
+ * earlier call is no longer the leaf, so its result would start a side branch that the current
69
+ * chain leaves out, with the calls and results written after that call. Such a result follows
70
+ * the leaf of its call's turn instead, which keeps one response's calls and all their results
71
+ * on the chain; the record after them follows the last result written, the leaf by then.
72
+ * @returns The record the result follows in the projection: the leaf, or its own call.
73
+ */
74
+ private __resultParent;
66
75
  private __hasEntry;
67
76
  private __unavailable;
68
77
  }
@@ -8,7 +8,7 @@ import { evidencePosition, buildEvidencePage, EVIDENCE_SOURCE_PAGE_SIZE, } from
8
8
  import { EntryRowSchema, EntrySeekSchema, FileRowSchema, RecordSchema, ApiRequestMetadataSchema, SeekSchema, } from './types.js';
9
9
  import { isClaudeAnswer, isClaudeCallFor, isClaudeRecordCwd, isClaudeWorkspaceCwd, openTranscript, projectDirectory, readClaudeParentUuid, toClaudeDetail, toClaudeImageData, toClaudeItem, } from './utilities.js';
10
10
  // identifies the path and record interpretation used by persisted projection entries
11
- const HISTORY_PROJECTION_VERSION = 7;
11
+ const HISTORY_PROJECTION_VERSION = 8;
12
12
  // one record, such as a tool result holding images, and an indexing read, which fits the largest
13
13
  const MAX_RECORD_BYTES = 8_388_608;
14
14
  const MAX_REFRESH_BYTES = 16_777_216;
@@ -40,7 +40,7 @@ export class ClaudeHistoryModel {
40
40
  constructor(database) {
41
41
  this.__database = database;
42
42
  this.__recordStatements = {
43
- parentTurn: database.prepare(`SELECT turn_id, turn_start_offset, anchor_id FROM hq_claude_entry
43
+ parentTurn: database.prepare(`SELECT turn_id, turn_start_offset, anchor_id, is_current FROM hq_claude_entry
44
44
  WHERE workspace_root = ? AND native_id = ? AND uuid = ?`),
45
45
  recordedParent: database.prepare('SELECT parent_uuid FROM hq_claude_entry WHERE workspace_root = ? AND native_id = ? AND uuid = ?'),
46
46
  upsert: database.prepare(`INSERT INTO hq_claude_entry (workspace_root, native_id, uuid, parent_uuid, turn_id, byte_offset,
@@ -703,26 +703,40 @@ export class ClaudeHistoryModel {
703
703
  this.__unavailable();
704
704
  const createdAt = Date.parse(record.timestamp);
705
705
  const statements = this.__recordStatements;
706
- const parentUuid = readClaudeParentUuid(record);
707
- const parent = parentUuid ? statements.parentTurn.get(root, nativeId, parentUuid) : undefined;
706
+ const nativeParentUuid = readClaudeParentUuid(record);
707
+ const nativeParent = nativeParentUuid
708
+ ? statements.parentTurn.get(root, nativeId, nativeParentUuid)
709
+ : undefined;
708
710
  // the CLI's presentation decides what is shown, the kind evidence filters on and where turns start
709
711
  const item = record.message !== undefined && !record.isMeta ? toClaudeItem(record, record.uuid) : null;
710
712
  const visible = item !== null;
711
- if (parentUuid === record.uuid)
713
+ if (nativeParentUuid === record.uuid)
712
714
  this.__unavailable();
713
- if (parentUuid && !parent) {
715
+ if (nativeParentUuid && !nativeParent) {
714
716
  if (visible)
715
717
  this.__unavailable();
716
718
  if (!isOrphanAllowed)
717
- return parentUuid;
719
+ return nativeParentUuid;
718
720
  }
721
+ const previous = statements.recordedParent.get(root, nativeId, record.uuid);
722
+ const isCallResult = item?.nativeType === 'tool-result' &&
723
+ nativeParentUuid !== null &&
724
+ record.sourceToolAssistantUUID === nativeParentUuid;
725
+ // a result indexed again keeps the record it was made to follow
726
+ const parentUuid = isCallResult && typeof previous?.parent_uuid === 'string'
727
+ ? previous.parent_uuid
728
+ : isCallResult && !previous
729
+ ? this.__resultParent(root, nativeId, row, nativeParentUuid, nativeParent)
730
+ : nativeParentUuid;
731
+ const parent = parentUuid === nativeParentUuid
732
+ ? nativeParent
733
+ : statements.parentTurn.get(root, nativeId, parentUuid);
719
734
  const textPrompt = item?.kind === 'user-message';
720
735
  const turnId = textPrompt
721
736
  ? record.uuid
722
737
  : typeof parent?.turn_id === 'string'
723
738
  ? parent.turn_id
724
739
  : record.uuid;
725
- const previous = statements.recordedParent.get(root, nativeId, record.uuid);
726
740
  if (previous && previous.parent_uuid !== parentUuid)
727
741
  this.__unavailable();
728
742
  const isAppend = row.leaf_id === null || parent?.anchor_id === row.leaf_id;
@@ -752,6 +766,23 @@ export class ClaudeHistoryModel {
752
766
  row.updated_at = Math.max(row.updated_at, createdAt);
753
767
  return null;
754
768
  }
769
+ /**
770
+ * Claude Code links a tool result to its own call. When parallel calls finish out of order, an
771
+ * earlier call is no longer the leaf, so its result would start a side branch that the current
772
+ * chain leaves out, with the calls and results written after that call. Such a result follows
773
+ * the leaf of its call's turn instead, which keeps one response's calls and all their results
774
+ * on the chain; the record after them follows the last result written, the leaf by then.
775
+ * @returns The record the result follows in the projection: the leaf, or its own call.
776
+ */
777
+ __resultParent(root, nativeId, row, callUuid, call) {
778
+ if (row.leaf_id === null ||
779
+ call?.is_current !== 1 ||
780
+ call.anchor_id !== callUuid ||
781
+ call.anchor_id === row.leaf_id)
782
+ return callUuid;
783
+ const leaf = this.__recordStatements.parentTurn.get(root, nativeId, row.leaf_id);
784
+ return leaf?.is_current === 1 && leaf.turn_id === call.turn_id ? row.leaf_id : callUuid;
785
+ }
755
786
  __hasEntry(root, nativeId, uuid) {
756
787
  return this.__recordStatements.hasEntry.get(root, nativeId, uuid) !== undefined;
757
788
  }
@@ -2288,6 +2288,7 @@ export declare const RecordSchema: z.ZodObject<{
2288
2288
  content: z.ZodUnknown;
2289
2289
  }, z.core.$strip>>;
2290
2290
  toolUseResult: z.ZodOptional<z.ZodUnknown>;
2291
+ sourceToolAssistantUUID: z.ZodOptional<z.ZodString>;
2291
2292
  }, z.core.$strip>;
2292
2293
  export type IRecord = z.infer<typeof RecordSchema>;
2293
2294
  export declare const FilePatchSchema: z.ZodObject<{
@@ -56,6 +56,8 @@ export const RecordSchema = z.object({
56
56
  message: z.object({ role: z.string(), content: z.unknown() }).optional(),
57
57
  // the CLI's own result metadata, such as an edit's patch, which live frames call tool_use_result
58
58
  toolUseResult: z.unknown().optional(),
59
+ // the call a tool result answers, which Claude Code also writes as the result's parent
60
+ sourceToolAssistantUUID: z.string().optional(),
59
61
  });
60
62
  // the file Claude Code records on an Edit or Write result; a created file's patch is empty
61
63
  export const FilePatchSchema = z.object({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jg-agent-hq",
3
- "version": "10.5.2",
3
+ "version": "10.5.4",
4
4
  "description": "Native Claude Code and Codex session control, history, usage and recoverable local storage for Node.js apps.",
5
5
  "type": "module",
6
6
  "license": "MIT",