@pontia/claude-client 0.8.5 → 0.8.6

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.
@@ -0,0 +1,100 @@
1
+ import { randomUUID } from "node:crypto";
2
+ export class Compaction {
3
+ emit;
4
+ active;
5
+ completed = new Set();
6
+ constructor(emit) {
7
+ this.emit = emit;
8
+ }
9
+ get busy() {
10
+ return this.active !== undefined;
11
+ }
12
+ request(operationId) {
13
+ const input = randomUUID();
14
+ this.active = { id: operationId, trigger: "manual", input, started: false };
15
+ return input;
16
+ }
17
+ accept(message) {
18
+ if ("parent_tool_use_id" in message && message.parent_tool_use_id)
19
+ return false;
20
+ if (message.type === "system" && message.subtype === "status") {
21
+ if (message.status === "compacting")
22
+ this.start();
23
+ if (message.compact_result === "failed" && this.active) {
24
+ this.fail(false, "Claude context compaction failed.");
25
+ if (!this.active.input)
26
+ this.finish();
27
+ }
28
+ }
29
+ if (message.type === "system" && message.subtype === "compact_boundary") {
30
+ if (this.completed.has(message.uuid))
31
+ return true;
32
+ this.completed.add(message.uuid);
33
+ const operation = this.start();
34
+ operation.terminal = this.fact("completed", {
35
+ native_compaction_id: message.uuid,
36
+ tokens_before: tokens(message.compact_metadata.pre_tokens),
37
+ tokens_after: tokens(message.compact_metadata.post_tokens),
38
+ });
39
+ if (!operation.input)
40
+ this.finish();
41
+ return true;
42
+ }
43
+ if (!this.active?.input)
44
+ return false;
45
+ // /compact produces local user frames and a model-less result, not a Pontia Turn.
46
+ if (message.type === "result") {
47
+ const ids = message.user_message_uuids ?? [message.user_message_uuid];
48
+ if (!ids.includes(this.active.input) ||
49
+ ids.some((id) => id !== this.active.input) ||
50
+ (message.user_message_uuid && message.user_message_uuid !== this.active.input))
51
+ throw new Error("compaction_result_unassociated");
52
+ if (!this.active.terminal) {
53
+ const aborted = message.terminal_reason === "aborted_streaming" ||
54
+ message.terminal_reason === "aborted_tools";
55
+ this.fail(aborted, message.is_error
56
+ ? "Claude context compaction failed."
57
+ : "Claude did not compact the context; there may be too few messages to summarize.");
58
+ }
59
+ this.finish();
60
+ }
61
+ return true;
62
+ }
63
+ closed(aborted) {
64
+ if (!this.active)
65
+ return;
66
+ this.fail(aborted, "Claude stopped before context compaction finished.");
67
+ this.finish();
68
+ }
69
+ start() {
70
+ this.active ??= { id: `compact_${randomUUID()}`, trigger: "unknown", started: false };
71
+ if (!this.active.started) {
72
+ this.active.started = true;
73
+ this.emit(this.fact("started"));
74
+ }
75
+ return this.active;
76
+ }
77
+ fact(phase, metadata = {}) {
78
+ return {
79
+ kind: "compaction",
80
+ phase,
81
+ data: {
82
+ operation_id: this.active.id,
83
+ trigger: this.active.trigger,
84
+ ...metadata,
85
+ },
86
+ };
87
+ }
88
+ fail(aborted, error) {
89
+ const operation = this.start();
90
+ operation.terminal ??= this.fact("failed", { aborted, error_message: error });
91
+ }
92
+ finish() {
93
+ const terminal = this.active.terminal;
94
+ this.active = undefined;
95
+ this.emit(terminal);
96
+ }
97
+ }
98
+ function tokens(value) {
99
+ return value !== undefined && Number.isSafeInteger(value) && value >= 0 ? value : null;
100
+ }
package/dist/src/host.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { query } from "@anthropic-ai/claude-agent-sdk";
2
+ import { Compaction } from "./compaction.js";
2
3
  import { Input } from "./input.js";
3
4
  import { Permissions } from "./permissions.js";
4
5
  import { FactError, InputFacts } from "./facts.js";
@@ -16,6 +17,7 @@ export class Host {
16
17
  emit;
17
18
  createQuery;
18
19
  input = new Input();
20
+ compaction;
19
21
  permissions;
20
22
  execution;
21
23
  reader;
@@ -34,6 +36,7 @@ export class Host {
34
36
  this.emit = emit;
35
37
  this.createQuery = createQuery;
36
38
  this.permissions = new Permissions(emit);
39
+ this.compaction = new Compaction(emit);
37
40
  }
38
41
  status() {
39
42
  return {
@@ -49,13 +52,13 @@ export class Host {
49
52
  isIdle() {
50
53
  if (!this.acceptsControl())
51
54
  throw new ControlError("rejected", "execution_unavailable");
52
- return !this.current || this.current.finished;
55
+ return !this.compaction.busy && (!this.current || this.current.finished);
53
56
  }
54
57
  submit(id, text) {
55
58
  if (!this.acceptsControl())
56
59
  throw new ControlError("rejected", "execution_unavailable");
57
60
  this.validateInput(id, text);
58
- if (this.current && !this.current.finished)
61
+ if (!this.isIdle())
59
62
  throw new ControlError("rejected", "input_busy");
60
63
  this.usedInputs.add(id);
61
64
  this.current = new InputFacts(id, this.emit);
@@ -118,6 +121,20 @@ export class Host {
118
121
  }
119
122
  }
120
123
  }
124
+ compact(operationId) {
125
+ if (!this.acceptsControl() || !this.execution || !this.native)
126
+ throw new ControlError("rejected", "execution_unavailable");
127
+ if (!this.isIdle())
128
+ throw new ControlError("rejected", "input_busy");
129
+ const id = this.compaction.request(operationId);
130
+ this.input.push({
131
+ type: "user",
132
+ uuid: id,
133
+ session_id: this.native,
134
+ parent_tool_use_id: null,
135
+ message: { role: "user", content: "/compact" },
136
+ });
137
+ }
121
138
  steer(target, id, text) {
122
139
  if (!this.acceptsControl())
123
140
  throw new ControlError("rejected", "execution_unavailable");
@@ -201,6 +218,7 @@ export class Host {
201
218
  if (this.unavailable)
202
219
  return;
203
220
  this.unavailable = reason;
221
+ this.compaction.closed(false);
204
222
  this.permissions.expire();
205
223
  this.emit({ kind: "unavailable", reason, ...(result ? { result } : {}) });
206
224
  }
@@ -223,6 +241,7 @@ export class Host {
223
241
  }
224
242
  }
225
243
  if (this.closing) {
244
+ this.compaction.closed(true);
226
245
  this.closed = true;
227
246
  this.emit({ kind: "closed", history_anchor: this.current?.historyAnchor });
228
247
  }
@@ -297,6 +316,13 @@ export class Host {
297
316
  });
298
317
  return;
299
318
  }
319
+ if (message.type === "system" &&
320
+ (message.subtype === "status" || message.subtype === "compact_boundary")) {
321
+ if (!this.native || message.session_id !== this.native)
322
+ throw new Error("native_identity_mismatch");
323
+ this.compaction.accept(message);
324
+ return;
325
+ }
300
326
  if (["stream_event", "assistant", "result", "user"].includes(message.type)) {
301
327
  if (!this.native || message.session_id !== this.native)
302
328
  throw new Error("native_identity_mismatch");
@@ -304,7 +330,8 @@ export class Host {
304
330
  this.retrying = false;
305
331
  this.emit({ kind: "retry", reason: null });
306
332
  }
307
- this.current?.accept(message);
333
+ if (!this.compaction.accept(message))
334
+ this.current?.accept(message);
308
335
  }
309
336
  }
310
337
  }
@@ -95,6 +95,9 @@ export async function serve(config, factory) {
95
95
  case "steer":
96
96
  host.steer(string(request.target_input_id), string(request.input_id), string(request.text));
97
97
  break;
98
+ case "compact":
99
+ host.compact(string(request.operation_id));
100
+ break;
98
101
  case "interrupt":
99
102
  await host.interrupt(string(request.input_id));
100
103
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pontia/claude-client",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "description": "Claude Code client for Pontia",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {