tinker-agent 2.8.0 → 2.10.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +79 -1
  2. package/README.md +81 -11
  3. package/package.json +5 -3
  4. package/src/agent/runtime-context-capabilities.ts +19 -0
  5. package/src/agent/runtime-context-events.ts +127 -0
  6. package/src/agent/runtime-context-maintenance.ts +780 -0
  7. package/src/agent/runtime-hosted-session.ts +443 -0
  8. package/src/agent/runtime-interactions.ts +291 -0
  9. package/src/agent/runtime-prompt-scheduler.ts +182 -0
  10. package/src/agent/runtime-session-contracts.ts +317 -0
  11. package/src/agent/runtime-session.ts +250 -2130
  12. package/src/agent/runtime-skills.ts +544 -0
  13. package/src/cli/command-line.ts +26 -2
  14. package/src/cli/connect-runner.tsx +26 -0
  15. package/src/cli/main.ts +26 -0
  16. package/src/cli/output.ts +1 -1
  17. package/src/cli/public-cli-contract.ts +18 -0
  18. package/src/cli/public-config-contract.ts +1 -1
  19. package/src/cli/runner-dependencies.ts +6 -5
  20. package/src/cli/serve-runner.ts +45 -0
  21. package/src/cli/serve-runtime.ts +100 -0
  22. package/src/context/context-automation-policy.ts +12 -118
  23. package/src/context/context-swap-renderer.ts +14 -0
  24. package/src/events/types.ts +12 -0
  25. package/src/memory/memory-get-tool.ts +1 -1
  26. package/src/observation/observation-builder.ts +128 -48
  27. package/src/remote/client.ts +350 -0
  28. package/src/remote/config.ts +95 -0
  29. package/src/remote/http-server.ts +240 -0
  30. package/src/remote/protocol.ts +228 -0
  31. package/src/remote/service-store.ts +175 -0
  32. package/src/remote/service.ts +219 -0
  33. package/src/remote/sync-hub.ts +95 -0
  34. package/src/session/remote-history-reader.ts +143 -0
  35. package/src/session/resume-projection.ts +47 -21
  36. package/src/session/session-history-access.ts +238 -0
  37. package/src/session/session-store-context-readers.ts +183 -0
  38. package/src/session/session-store-ledger-writer.ts +315 -0
  39. package/src/session/session-store-record-writer.ts +318 -0
  40. package/src/session/session-store-recovery.ts +225 -0
  41. package/src/session/session-store-revisions.ts +1004 -0
  42. package/src/session/session-store-sql.ts +40 -0
  43. package/src/session/session-store-validation.ts +657 -0
  44. package/src/session/session-store.ts +756 -3186
  45. package/src/tools/bash-task.ts +46 -18
  46. package/src/tools/bash.ts +44 -2
  47. package/src/tools/glob.ts +107 -19
  48. package/src/tools/grep-output.ts +130 -0
  49. package/src/tools/grep-pagination.ts +73 -0
  50. package/src/tools/grep-path.ts +11 -0
  51. package/src/tools/grep-snippets.ts +111 -0
  52. package/src/tools/grep.ts +139 -154
  53. package/src/tools/read.ts +0 -9
  54. package/src/tools/recall.ts +106 -50
  55. package/src/tools/registry.ts +4 -6
  56. package/src/tools/ripgrep.ts +19 -26
  57. package/src/tools/shell-process.ts +30 -4
  58. package/src/tools/task-output-range.ts +146 -0
  59. package/src/tools/task-output-tool.ts +35 -5
  60. package/src/tools/task-output.ts +35 -0
  61. package/src/tools/task-stop.ts +2 -1
  62. package/src/tools/task-tool-args.ts +34 -0
  63. package/src/tools/terminal-screen.ts +11 -2
  64. package/src/tools/types.ts +39 -2
  65. package/src/tui/event-store.ts +23 -5
  66. package/src/tui/remote-app.tsx +210 -0
@@ -0,0 +1,225 @@
1
+ import type { Database } from "bun:sqlite";
2
+ import {
3
+ canonicalToolResultContentHash,
4
+ toolResultDisplayText,
5
+ } from "../agent/tool-result-content";
6
+ import { ContextProtocolValidator } from "../context/context-protocol-validator";
7
+ import {
8
+ immutableRecord,
9
+ interruptedCompletionInputs,
10
+ observationForCompletion,
11
+ type CanonicalMessageRecord,
12
+ type ProtocolContextView,
13
+ type ProtocolFrame,
14
+ type ToolCompletion,
15
+ type ToolResultRecord,
16
+ } from "../context/protocol-frame";
17
+ import type {
18
+ IterationId,
19
+ RuntimeIdFactory,
20
+ SessionId,
21
+ TurnId,
22
+ } from "../ids/runtime-id";
23
+ import { stableJsonStringify } from "../model/model-request-preflight";
24
+ import { SessionError } from "./session-errors";
25
+ import type { SessionStore } from "./session-store";
26
+ import { type SessionRecoveryResult } from "./session-store-contracts";
27
+ import type { SessionStoreLedgerWriter } from "./session-store-ledger-writer";
28
+ import { insertMessage, insertToolResult } from "./session-store-record-writer";
29
+ import { requireItem, requireSingleChange, runTransaction } from "./session-store-sql";
30
+
31
+ /** Repairs only the interrupted canonical tail before the session is resumed. */
32
+ export class SessionStoreRecovery {
33
+ private readonly validator = new ContextProtocolValidator();
34
+ constructor(
35
+ private readonly database: Database,
36
+ private readonly sessionId: SessionId,
37
+ private readonly clock: () => string,
38
+ private readonly requireOpen: () => void,
39
+ private readonly ledgerWriter: Pick<
40
+ SessionStoreLedgerWriter,
41
+ "markOpenTurnInterrupted" | "markTerminalRows"
42
+ >,
43
+ private readonly store: Pick<SessionStore, "loadProtocolView" | "validateAll">,
44
+ ) {}
45
+
46
+ recoverInterruptedState(
47
+ idFactory: RuntimeIdFactory,
48
+ recallIndexRebuilt: boolean,
49
+ ): SessionRecoveryResult {
50
+ this.requireOpen();
51
+ const view = this.store.loadProtocolView();
52
+ const openTurns = this.database
53
+ .query("SELECT turn_id FROM turns WHERE status = 'open' ORDER BY turn_number")
54
+ .all() as Array<{ turn_id: string }>;
55
+ const openFrames = view.frames.filter((frame) => frame.state === "open");
56
+ if (openTurns.length === 0 && openFrames.length === 0) {
57
+ return {
58
+ syntheticCompletionCount: 0,
59
+ recallIndexRebuilt,
60
+ };
61
+ }
62
+ if (openTurns.length !== 1 || openFrames.length > 1) {
63
+ throw this.recoveryError(
64
+ "Session has an invalid number of open turns or frames.",
65
+ );
66
+ }
67
+ const turnId = openTurns[0].turn_id as TurnId;
68
+ const openIterations = this.database
69
+ .query(
70
+ "SELECT iteration_id FROM iterations WHERE turn_id = ? AND outcome = 'open' ORDER BY iteration_number",
71
+ )
72
+ .all(turnId) as Array<{ iteration_id: string }>;
73
+ if (openIterations.length > 1) {
74
+ throw this.recoveryError(`Turn ${turnId} has multiple open iterations.`);
75
+ }
76
+
77
+ const frame = openFrames[0];
78
+ if (frame === undefined) {
79
+ this.ledgerWriter.markOpenTurnInterrupted(
80
+ turnId,
81
+ openIterations[0]?.iteration_id as IterationId | undefined,
82
+ );
83
+ this.store.validateAll({ allowOpenTail: false });
84
+ return {
85
+ recoveredTurnId: turnId,
86
+ syntheticCompletionCount: 0,
87
+ recallIndexRebuilt,
88
+ };
89
+ }
90
+ if (
91
+ frame.turnId !== turnId ||
92
+ frame !== view.frames.at(-1) ||
93
+ openIterations.length !== 1 ||
94
+ frame.iterationId !== openIterations[0]?.iteration_id
95
+ ) {
96
+ throw this.recoveryError(`Open frame ${frame.frameId} has invalid ownership.`);
97
+ }
98
+
99
+ const frameMessages = view.messages.filter(
100
+ (message) => message.frameId === frame.frameId,
101
+ );
102
+ const assistant = frameMessages[0];
103
+ if (assistant?.role !== "assistant" || assistant.toolCalls === undefined) {
104
+ throw this.recoveryError(`Open frame ${frame.frameId} has no tool calls.`);
105
+ }
106
+ const missingCalls = assistant.toolCalls.slice(frameMessages.length - 1);
107
+ if (missingCalls.length === 0) {
108
+ throw this.recoveryError(`Open frame ${frame.frameId} has no missing call.`);
109
+ }
110
+ const completionInputs = interruptedCompletionInputs(missingCalls);
111
+ const messages: CanonicalMessageRecord[] = [];
112
+ const toolResults: ToolResultRecord[] = [];
113
+ for (const input of completionInputs) {
114
+ const createdAt = this.clock();
115
+ const content = observationForCompletion(input);
116
+ const displayText = toolResultDisplayText(content);
117
+ const messageId = idFactory.createMessageId();
118
+ const message = immutableRecord<CanonicalMessageRecord>({
119
+ messageId,
120
+ sessionId: this.sessionId,
121
+ frameId: frame.frameId,
122
+ ordinal: view.messages.length + messages.length + 1,
123
+ contentSha256: canonicalToolResultContentHash(content),
124
+ createdAt,
125
+ role: "tool",
126
+ turnId,
127
+ iterationId: frame.iterationId,
128
+ toolCallId: input.call.toolCallId,
129
+ providerToolCallId: input.call.providerToolCallId,
130
+ name: input.call.name,
131
+ content,
132
+ displayText,
133
+ origin: "runtime",
134
+ });
135
+ const completion: ToolCompletion = immutableRecord({
136
+ kind: "synthetic",
137
+ reason: input.reason,
138
+ });
139
+ const result = immutableRecord<ToolResultRecord>({
140
+ sessionId: this.sessionId,
141
+ frameId: frame.frameId,
142
+ toolCallId: input.call.toolCallId,
143
+ toolMessageId: messageId,
144
+ completion,
145
+ observationSha256: canonicalToolResultContentHash(content),
146
+ createdAt,
147
+ });
148
+ messages.push(message);
149
+ toolResults.push(result);
150
+ }
151
+ const closedAt = this.clock();
152
+ const closedFrame = immutableRecord<ProtocolFrame>({
153
+ ...frame,
154
+ state: "closed",
155
+ lastOrdinal: view.messages.length + messages.length,
156
+ closedAt,
157
+ });
158
+ const candidate: ProtocolContextView = Object.freeze({
159
+ ...view,
160
+ frames: Object.freeze(
161
+ view.frames.map((entry) =>
162
+ entry.frameId === frame.frameId ? closedFrame : entry,
163
+ ),
164
+ ),
165
+ messages: Object.freeze([...view.messages, ...messages]),
166
+ toolResults: Object.freeze([...view.toolResults, ...toolResults]),
167
+ });
168
+ this.validator.validate(candidate, { fullIntegrity: true });
169
+
170
+ try {
171
+ runTransaction(this.database, () => {
172
+ for (let index = 0; index < messages.length; index += 1) {
173
+ insertMessage(
174
+ this.database,
175
+ requireItem(messages, index, "recovery message"),
176
+ );
177
+ insertToolResult(
178
+ this.database,
179
+ requireItem(toolResults, index, "recovery tool result"),
180
+ );
181
+ }
182
+ const frameUpdate = this.database
183
+ .query(
184
+ `UPDATE protocol_frames SET state = 'closed', last_ordinal = ?, closed_at = ?
185
+ WHERE frame_id = ? AND state = 'open' AND last_ordinal IS NULL`,
186
+ )
187
+ .run(closedFrame.lastOrdinal!, closedAt, frame.frameId);
188
+ requireSingleChange(
189
+ this.database,
190
+ frameUpdate.changes,
191
+ "close recovered frame",
192
+ );
193
+ this.ledgerWriter.markTerminalRows(
194
+ turnId,
195
+ frame.iterationId!,
196
+ "interrupted",
197
+ "interrupted",
198
+ null,
199
+ stableJsonStringify({ version: 1, reason: "process_interrupted" }),
200
+ closedAt,
201
+ );
202
+ });
203
+ } catch (error) {
204
+ throw new SessionError(
205
+ "SESSION_RECOVERY_FAILED",
206
+ "recover_open_frame",
207
+ `Failed to recover open frame ${frame.frameId}.`,
208
+ { sessionId: this.sessionId, frameId: frame.frameId, cause: error },
209
+ );
210
+ }
211
+ this.store.validateAll({ allowOpenTail: false });
212
+ return {
213
+ recoveredTurnId: turnId,
214
+ recoveredFrameId: frame.frameId,
215
+ syntheticCompletionCount: messages.length,
216
+ recallIndexRebuilt,
217
+ };
218
+ }
219
+
220
+ private recoveryError(message: string): SessionError {
221
+ return new SessionError("SESSION_RECOVERY_FAILED", "recover_session", message, {
222
+ sessionId: this.sessionId,
223
+ });
224
+ }
225
+ }