@keyou007/agent-bot 0.1.25-alpha.1 → 0.1.25
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 +29 -0
- package/README.md +34 -6
- package/README.zh.md +34 -6
- package/config.example.yaml +1 -3
- package/dist/cli/help.js +2 -0
- package/dist/cli/help.js.map +1 -1
- package/dist/codex/CodexRuntime.js +15 -5
- package/dist/codex/CodexRuntime.js.map +1 -1
- package/dist/commands/CommandRouter.js +3 -0
- package/dist/commands/CommandRouter.js.map +1 -1
- package/dist/config/SettingsStore.js +341 -0
- package/dist/config/SettingsStore.js.map +1 -0
- package/dist/config/loadConfig.js +1 -2
- package/dist/config/loadConfig.js.map +1 -1
- package/dist/config/schema.js +0 -1
- package/dist/config/schema.js.map +1 -1
- package/dist/console/ConsoleTurnPresenter.js +3 -0
- package/dist/console/ConsoleTurnPresenter.js.map +1 -1
- package/dist/feishu/CardRenderer.js +34 -99
- package/dist/feishu/CardRenderer.js.map +1 -1
- package/dist/feishu/FeishuConnector.js +9 -0
- package/dist/feishu/FeishuConnector.js.map +1 -1
- package/dist/feishu/FeishuMessageClient.js +23 -18
- package/dist/feishu/FeishuMessageClient.js.map +1 -1
- package/dist/feishu/FeishuTurnPresenter.js +36 -23
- package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
- package/dist/feishu/LocalCardImage.js +0 -4
- package/dist/feishu/LocalCardImage.js.map +1 -1
- package/dist/feishu/LocalImageMarkdown.js +59 -3
- package/dist/feishu/LocalImageMarkdown.js.map +1 -1
- package/dist/feishu/SettingsCard.js +199 -0
- package/dist/feishu/SettingsCard.js.map +1 -0
- package/dist/index.js +3 -6
- package/dist/index.js.map +1 -1
- package/dist/local-files/TurnPreviewPage.js +8 -4
- package/dist/local-files/TurnPreviewPage.js.map +1 -1
- package/dist/presentation/TurnStateReducer.js +21 -2
- package/dist/presentation/TurnStateReducer.js.map +1 -1
- package/dist/presentation/turnStatus.js +34 -0
- package/dist/presentation/turnStatus.js.map +1 -0
- package/dist/proxy/ProxySessionController.js +409 -163
- package/dist/proxy/ProxySessionController.js.map +1 -1
- package/dist/runtime/submissionErrors.js +15 -0
- package/dist/runtime/submissionErrors.js.map +1 -0
- package/dist/state/ExecutionProvenanceStore.js +58 -0
- package/dist/state/ExecutionProvenanceStore.js.map +1 -0
- package/dist/state/StateStore.js +96 -33
- package/dist/state/StateStore.js.map +1 -1
- package/dist/state/migrations.js +12 -0
- package/dist/state/migrations.js.map +1 -1
- package/docs/technical-reference.md +11 -8
- package/docs/technical-reference.zh.md +11 -8
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/agent-bot/SKILL.md +9 -3
- package/src/cli/help.ts +2 -0
- package/src/codex/CodexRuntime.ts +14 -5
- package/src/commands/CommandRouter.ts +3 -0
- package/src/commands/commandTypes.ts +1 -0
- package/src/config/SettingsStore.ts +588 -0
- package/src/config/loadConfig.ts +1 -2
- package/src/config/schema.ts +0 -1
- package/src/console/ConsoleTurnPresenter.ts +2 -0
- package/src/feishu/CardRenderer.ts +35 -114
- package/src/feishu/FeishuConnector.ts +9 -0
- package/src/feishu/FeishuMessageClient.ts +23 -22
- package/src/feishu/FeishuTurnPresenter.ts +33 -22
- package/src/feishu/LocalCardImage.ts +0 -6
- package/src/feishu/LocalImageMarkdown.ts +54 -3
- package/src/feishu/SettingsCard.ts +231 -0
- package/src/index.ts +3 -6
- package/src/local-files/TurnPreviewPage.ts +8 -4
- package/src/presentation/TurnStateReducer.ts +21 -2
- package/src/presentation/turnStatus.ts +38 -0
- package/src/presentation/turnViewTypes.ts +3 -0
- package/src/proxy/ProxySessionController.ts +382 -162
- package/src/runtime/submissionErrors.ts +15 -0
- package/src/runtime/types.ts +1 -0
- package/src/state/ExecutionProvenanceStore.ts +100 -0
- package/src/state/StateStore.ts +118 -33
- package/src/state/migrations.ts +12 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Only raise this before dispatching a prompt-bearing request. A timeout,
|
|
2
|
+
// disconnect, or generic provider rejection is not evidence of non-submission.
|
|
3
|
+
export class TurnNotSubmittedError extends Error {
|
|
4
|
+
constructor(cause: unknown) {
|
|
5
|
+
super(cause instanceof Error ? cause.message : String(cause), { cause });
|
|
6
|
+
this.name = "TurnNotSubmittedError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class TurnStoppedBeforeSubmissionError extends TurnNotSubmittedError {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(new Error("本次执行已停止,未提交请求。"));
|
|
13
|
+
this.name = "TurnStoppedBeforeSubmissionError";
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/runtime/types.ts
CHANGED
|
@@ -113,6 +113,7 @@ export type AgentEvent =
|
|
|
113
113
|
| { type: "approval_resolved"; sessionId: string; turnId: string; requestId: string; decision: ApprovalDecision; automatic?: boolean }
|
|
114
114
|
| { type: "turn_completed"; sessionId: string; turnId: string; finalResponse: string; durationMs?: number; approvedPlan?: boolean }
|
|
115
115
|
| { type: "turn_cancelled"; sessionId: string; turnId: string }
|
|
116
|
+
| { type: "turn_status_unknown"; sessionId: string; turnId: string; message: string }
|
|
116
117
|
| { type: "turn_failed"; sessionId: string; turnId: string; message: string };
|
|
117
118
|
|
|
118
119
|
export interface SessionMetadataUpdatedEvent {
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type Database from "better-sqlite3";
|
|
2
|
+
import type { RuntimeSession } from "../runtime/types.js";
|
|
3
|
+
import { createId } from "../utils/id.js";
|
|
4
|
+
|
|
5
|
+
export type ExecutionTrigger = "user" | "queue" | "schedule" | "recovery" | "retry"
|
|
6
|
+
| "plan_handoff" | "response_continuation";
|
|
7
|
+
|
|
8
|
+
export interface ExecutionProvenance {
|
|
9
|
+
submissionId: string;
|
|
10
|
+
instructionId: string;
|
|
11
|
+
attemptId: string;
|
|
12
|
+
localSessionId: string;
|
|
13
|
+
turnId?: string;
|
|
14
|
+
trigger: ExecutionTrigger;
|
|
15
|
+
parentSourceTurnId?: string;
|
|
16
|
+
scheduledFor?: number;
|
|
17
|
+
submittedAt: number;
|
|
18
|
+
settings: Pick<RuntimeSession, "agentName" | "modelProvider" | "model" | "reasoningEffort" | "permissionMode" | "cwd">;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ProvenanceRow {
|
|
22
|
+
submission_id: string;
|
|
23
|
+
instruction_id: string;
|
|
24
|
+
attempt_id: string;
|
|
25
|
+
local_session_id: string;
|
|
26
|
+
turn_id: string | null;
|
|
27
|
+
trigger: ExecutionTrigger;
|
|
28
|
+
parent_source_turn_id: string | null;
|
|
29
|
+
scheduled_for: number | null;
|
|
30
|
+
submitted_at: number;
|
|
31
|
+
agent_name: string;
|
|
32
|
+
model_provider: string | null;
|
|
33
|
+
model: string | null;
|
|
34
|
+
reasoning_effort: string | null;
|
|
35
|
+
permission_mode: RuntimeSession["permissionMode"];
|
|
36
|
+
cwd: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class ExecutionProvenanceStore {
|
|
40
|
+
constructor(private readonly db: Database.Database) {}
|
|
41
|
+
|
|
42
|
+
recordSubmission(input: Omit<ExecutionProvenance, "submissionId" | "submittedAt" | "turnId" | "instructionId">
|
|
43
|
+
& { instructionId?: string }): ExecutionProvenance {
|
|
44
|
+
const previous = this.latestForAttempt(input.attemptId);
|
|
45
|
+
const parent = input.parentSourceTurnId && input.trigger !== "user" && input.trigger !== "queue" && input.trigger !== "schedule"
|
|
46
|
+
? this.forTurn(input.parentSourceTurnId, input.localSessionId) : undefined;
|
|
47
|
+
const instructionId = previous?.instructionId ?? parent?.instructionId ?? input.instructionId ?? input.attemptId;
|
|
48
|
+
const scheduledFor = input.scheduledFor ?? previous?.scheduledFor ?? parent?.scheduledFor;
|
|
49
|
+
const submissionId = createId("submission");
|
|
50
|
+
// Explicit columns are the allowlist: never serialize a runtime session or prompt here.
|
|
51
|
+
const { agentName, modelProvider, model, reasoningEffort, permissionMode, cwd } = input.settings;
|
|
52
|
+
this.db.prepare(`INSERT INTO execution_provenance (
|
|
53
|
+
submission_id, instruction_id, attempt_id, local_session_id, trigger, parent_source_turn_id,
|
|
54
|
+
scheduled_for, submitted_at, agent_name, model_provider, model, reasoning_effort, permission_mode, cwd
|
|
55
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(
|
|
56
|
+
submissionId, instructionId, input.attemptId, input.localSessionId, input.trigger, input.parentSourceTurnId ?? null,
|
|
57
|
+
scheduledFor ?? null, Date.now(), agentName, modelProvider ?? null, model ?? null, reasoningEffort ?? null, permissionMode, cwd,
|
|
58
|
+
);
|
|
59
|
+
return this.latestForAttempt(input.attemptId)!;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
bindTurn(submissionId: string, turnId: string): void {
|
|
63
|
+
this.db.prepare(`UPDATE execution_provenance SET turn_id = ? WHERE submission_id = ? AND turn_id IS NULL`)
|
|
64
|
+
.run(turnId, submissionId);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
bindLatestTurn(attemptId: string, turnId: string): void {
|
|
68
|
+
const latest = this.latestForAttempt(attemptId);
|
|
69
|
+
if (latest) this.bindTurn(latest.submissionId, turnId);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
latestForAttempt(attemptId: string): ExecutionProvenance | undefined {
|
|
73
|
+
return this.read(this.db.prepare(`SELECT * FROM execution_provenance WHERE attempt_id = ? ORDER BY rowid DESC LIMIT 1`)
|
|
74
|
+
.get(attemptId));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
forTurn(turnId: string, localSessionId: string): ExecutionProvenance | undefined {
|
|
78
|
+
return this.read(this.db.prepare(`SELECT * FROM execution_provenance WHERE turn_id = ? AND local_session_id = ?
|
|
79
|
+
ORDER BY rowid DESC LIMIT 1`).get(turnId, localSessionId));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
listForAttempt(attemptId: string): ExecutionProvenance[] {
|
|
83
|
+
return this.db.prepare(`SELECT * FROM execution_provenance WHERE attempt_id = ? ORDER BY rowid`).all(attemptId)
|
|
84
|
+
.map((row) => this.read(row)!);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private read(value: unknown): ExecutionProvenance | undefined {
|
|
88
|
+
if (!value) return undefined;
|
|
89
|
+
const row = value as ProvenanceRow;
|
|
90
|
+
return {
|
|
91
|
+
submissionId: row.submission_id, instructionId: row.instruction_id, attemptId: row.attempt_id,
|
|
92
|
+
localSessionId: row.local_session_id, turnId: row.turn_id ?? undefined, trigger: row.trigger,
|
|
93
|
+
parentSourceTurnId: row.parent_source_turn_id ?? undefined, scheduledFor: row.scheduled_for ?? undefined,
|
|
94
|
+
submittedAt: row.submitted_at, settings: {
|
|
95
|
+
agentName: row.agent_name, modelProvider: row.model_provider ?? undefined, model: row.model ?? undefined,
|
|
96
|
+
reasoningEffort: row.reasoning_effort ?? undefined, permissionMode: row.permission_mode, cwd: row.cwd,
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/state/StateStore.ts
CHANGED
|
@@ -6,6 +6,7 @@ import path from "node:path";
|
|
|
6
6
|
import Database from "better-sqlite3";
|
|
7
7
|
import { ScheduledTaskStore } from "../scheduling/ScheduledTaskStore.js";
|
|
8
8
|
import { migrations } from "./migrations.js";
|
|
9
|
+
import { ExecutionProvenanceStore } from "./ExecutionProvenanceStore.js";
|
|
9
10
|
import type { UpdateCheckSchedule, UpdateNotice } from "../updates/types.js";
|
|
10
11
|
import type { ConversationTurn, RuntimeExecutionSettings } from "../runtime/types.js";
|
|
11
12
|
|
|
@@ -174,7 +175,10 @@ export type TurnAttemptStatus =
|
|
|
174
175
|
| "completed"
|
|
175
176
|
| "failed"
|
|
176
177
|
| "cancelled"
|
|
177
|
-
| "interrupted"
|
|
178
|
+
| "interrupted"
|
|
179
|
+
| "unknown";
|
|
180
|
+
|
|
181
|
+
export type TurnSubmissionState = "not_submitted" | "submission_unknown" | "submitted";
|
|
178
182
|
|
|
179
183
|
export interface TurnAttemptRecord {
|
|
180
184
|
attemptId: string;
|
|
@@ -190,6 +194,8 @@ export interface TurnAttemptRecord {
|
|
|
190
194
|
recoveryCount: number;
|
|
191
195
|
retryCount: number;
|
|
192
196
|
status: TurnAttemptStatus;
|
|
197
|
+
submissionState: TurnSubmissionState;
|
|
198
|
+
executionGeneration: number;
|
|
193
199
|
createdAt: string;
|
|
194
200
|
updatedAt: string;
|
|
195
201
|
}
|
|
@@ -266,6 +272,7 @@ interface QueuedPromptRow {
|
|
|
266
272
|
}
|
|
267
273
|
|
|
268
274
|
interface TurnAttemptRow {
|
|
275
|
+
execution_generation: number;
|
|
269
276
|
attempt_id: string;
|
|
270
277
|
local_session_id: string;
|
|
271
278
|
context_key: string;
|
|
@@ -279,6 +286,7 @@ interface TurnAttemptRow {
|
|
|
279
286
|
recovery_count: number;
|
|
280
287
|
retry_count: number;
|
|
281
288
|
status: TurnAttemptStatus;
|
|
289
|
+
submission_state: TurnSubmissionState;
|
|
282
290
|
created_at: string;
|
|
283
291
|
updated_at: string;
|
|
284
292
|
}
|
|
@@ -287,6 +295,7 @@ export class StateStore {
|
|
|
287
295
|
private readonly db: Database.Database;
|
|
288
296
|
readonly previews: TurnPreviewJournal;
|
|
289
297
|
readonly schedules: ScheduledTaskStore;
|
|
298
|
+
readonly provenance: ExecutionProvenanceStore;
|
|
290
299
|
|
|
291
300
|
constructor(sqlitePath: string, onPreviewError?: (error: unknown) => void) {
|
|
292
301
|
this.previews = new TurnPreviewJournal(path.join(path.dirname(sqlitePath), "turn-previews"), onPreviewError);
|
|
@@ -303,7 +312,9 @@ export class StateStore {
|
|
|
303
312
|
this.ensureChatContextColumns();
|
|
304
313
|
this.ensureTurnAttemptColumns();
|
|
305
314
|
this.ensureQueuedPromptColumns();
|
|
315
|
+
this.ensureRecentContextReceiptColumns();
|
|
306
316
|
this.schedules = new ScheduledTaskStore(this.db);
|
|
317
|
+
this.provenance = new ExecutionProvenanceStore(this.db);
|
|
307
318
|
this.initializeContextSessionMappings();
|
|
308
319
|
this.db.exec("DROP INDEX IF EXISTS idx_sessions_remote_session_id_unique");
|
|
309
320
|
this.reconcileDuplicateRemoteSessions();
|
|
@@ -599,8 +610,9 @@ export class StateStore {
|
|
|
599
610
|
localImagePaths?: string[];
|
|
600
611
|
messageId?: string;
|
|
601
612
|
replyMessageId?: string;
|
|
613
|
+
createdAt?: string;
|
|
602
614
|
}): QueuedPromptRecord {
|
|
603
|
-
const createdAt = new Date().toISOString();
|
|
615
|
+
const createdAt = input.createdAt ?? new Date().toISOString();
|
|
604
616
|
this.db.prepare(`
|
|
605
617
|
INSERT INTO queued_prompts (
|
|
606
618
|
prompt_id, local_session_id, context_key, prompt_text, display_prompt, local_image_paths_json,
|
|
@@ -673,8 +685,18 @@ export class StateStore {
|
|
|
673
685
|
this.db.prepare("DELETE FROM queued_prompts WHERE prompt_id = ?").run(row.prompt_id);
|
|
674
686
|
if (!this.schedules.claimStart(row.prompt_id)) continue;
|
|
675
687
|
if (this.schedules.getRun(row.prompt_id)) {
|
|
676
|
-
this.
|
|
677
|
-
|
|
688
|
+
const existing = this.getTurnAttempt(row.prompt_id);
|
|
689
|
+
if (existing) {
|
|
690
|
+
if (existing.submissionState !== "not_submitted" || existing.turnId) {
|
|
691
|
+
this.schedules.finish(row.prompt_id, "unknown", "存在无法安全重发的提交记录,未自动派发。");
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
this.updateTurnAttempt(row.prompt_id, { status: "accepted", pendingTurnId: null,
|
|
695
|
+
executionGeneration: this.executionGeneration(localSessionId) });
|
|
696
|
+
} else {
|
|
697
|
+
this.createTurnAttempt({ attemptId: row.prompt_id, localSessionId, contextKey: row.context_key,
|
|
698
|
+
promptText: row.prompt_text, replyMessageId: row.reply_message_id ?? undefined });
|
|
699
|
+
}
|
|
678
700
|
}
|
|
679
701
|
return mapQueuedPrompt(row);
|
|
680
702
|
}
|
|
@@ -709,6 +731,8 @@ export class StateStore {
|
|
|
709
731
|
recoveredFromTurnId?: string;
|
|
710
732
|
recoveryCount?: number;
|
|
711
733
|
retryCount?: number;
|
|
734
|
+
submissionState?: TurnSubmissionState;
|
|
735
|
+
executionGeneration?: number;
|
|
712
736
|
status?: Extract<TurnAttemptStatus, "accepted" | "running" | "recovering">;
|
|
713
737
|
createdAt?: string;
|
|
714
738
|
updatedAt?: string;
|
|
@@ -720,8 +744,8 @@ export class StateStore {
|
|
|
720
744
|
INSERT INTO turn_attempts (
|
|
721
745
|
attempt_id, local_session_id, context_key, prompt_text, local_image_paths_json,
|
|
722
746
|
message_id, reply_message_id, pending_turn_id, turn_id, recovered_from_turn_id,
|
|
723
|
-
recovery_count, retry_count, status, created_at, updated_at
|
|
724
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
747
|
+
recovery_count, retry_count, submission_state, status, created_at, updated_at, execution_generation
|
|
748
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
725
749
|
`).run(
|
|
726
750
|
input.attemptId,
|
|
727
751
|
input.localSessionId,
|
|
@@ -735,9 +759,11 @@ export class StateStore {
|
|
|
735
759
|
input.recoveredFromTurnId ?? null,
|
|
736
760
|
input.recoveryCount ?? 0,
|
|
737
761
|
input.retryCount ?? 0,
|
|
762
|
+
input.submissionState ?? (input.turnId ? "submitted" : "not_submitted"),
|
|
738
763
|
status,
|
|
739
764
|
createdAt,
|
|
740
765
|
updatedAt,
|
|
766
|
+
input.executionGeneration ?? this.executionGeneration(input.localSessionId),
|
|
741
767
|
);
|
|
742
768
|
return this.getTurnAttempt(input.attemptId)!;
|
|
743
769
|
}
|
|
@@ -792,6 +818,8 @@ export class StateStore {
|
|
|
792
818
|
recoveredFromTurnId: string | null;
|
|
793
819
|
recoveryCount: number;
|
|
794
820
|
retryCount: number;
|
|
821
|
+
submissionState: TurnSubmissionState;
|
|
822
|
+
executionGeneration: number;
|
|
795
823
|
status: TurnAttemptStatus;
|
|
796
824
|
}>,
|
|
797
825
|
): void {
|
|
@@ -802,7 +830,7 @@ export class StateStore {
|
|
|
802
830
|
this.db.prepare(`
|
|
803
831
|
UPDATE turn_attempts
|
|
804
832
|
SET message_id = ?, reply_message_id = ?, pending_turn_id = ?, turn_id = ?,
|
|
805
|
-
recovered_from_turn_id = ?, recovery_count = ?, retry_count = ?, status = ?, updated_at = ?
|
|
833
|
+
recovered_from_turn_id = ?, recovery_count = ?, retry_count = ?, submission_state = ?, execution_generation = ?, status = ?, updated_at = ?
|
|
806
834
|
WHERE attempt_id = ?
|
|
807
835
|
`).run(
|
|
808
836
|
value(patch.messageId, existing.messageId),
|
|
@@ -812,6 +840,8 @@ export class StateStore {
|
|
|
812
840
|
value(patch.recoveredFromTurnId, existing.recoveredFromTurnId),
|
|
813
841
|
patch.recoveryCount ?? existing.recoveryCount,
|
|
814
842
|
patch.retryCount ?? existing.retryCount,
|
|
843
|
+
patch.submissionState ?? (patch.turnId ? "submitted" : existing.submissionState),
|
|
844
|
+
patch.executionGeneration ?? existing.executionGeneration,
|
|
815
845
|
patch.status ?? existing.status,
|
|
816
846
|
new Date().toISOString(),
|
|
817
847
|
attemptId,
|
|
@@ -821,7 +851,7 @@ export class StateStore {
|
|
|
821
851
|
bindTurnAttempt(localSessionId: string, turnId: string): TurnAttemptRecord | undefined {
|
|
822
852
|
const attempt = this.findIncompleteTurnAttemptForSession(localSessionId);
|
|
823
853
|
if (!attempt || (attempt.turnId && attempt.turnId !== turnId)) return undefined;
|
|
824
|
-
this.updateTurnAttempt(attempt.attemptId, { turnId, status: "running" });
|
|
854
|
+
this.updateTurnAttempt(attempt.attemptId, { turnId, status: "running", submissionState: "submitted" });
|
|
825
855
|
return this.getTurnAttempt(attempt.attemptId);
|
|
826
856
|
}
|
|
827
857
|
|
|
@@ -856,12 +886,6 @@ export class StateStore {
|
|
|
856
886
|
return existing;
|
|
857
887
|
}
|
|
858
888
|
|
|
859
|
-
restoreTurnAttemptAfterCancellationFailure(attempt: TurnAttemptRecord): void {
|
|
860
|
-
const current = this.getTurnAttempt(attempt.attemptId);
|
|
861
|
-
if (current?.status !== "cancelling") return;
|
|
862
|
-
this.updateTurnAttempt(attempt.attemptId, { status: attempt.status });
|
|
863
|
-
}
|
|
864
|
-
|
|
865
889
|
touchTurnAttempt(turnId: string, activityAt = new Date()): void {
|
|
866
890
|
const timestamp = activityAt.toISOString();
|
|
867
891
|
const throttleCutoff = new Date(activityAt.getTime() - 15_000).toISOString();
|
|
@@ -874,45 +898,85 @@ export class StateStore {
|
|
|
874
898
|
`).run(timestamp, turnId, throttleCutoff);
|
|
875
899
|
}
|
|
876
900
|
|
|
877
|
-
|
|
901
|
+
hasUnknownTurnAttemptForSession(localSessionId: string): boolean {
|
|
902
|
+
return Boolean(this.db.prepare("SELECT 1 FROM turn_attempts WHERE local_session_id = ? AND status = 'unknown' LIMIT 1")
|
|
903
|
+
.get(localSessionId));
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
hasCancellationRequestedForSession(localSessionId: string): boolean {
|
|
907
|
+
return Boolean(this.db.prepare("SELECT 1 FROM turn_attempts WHERE local_session_id = ? AND status = 'cancelling' LIMIT 1")
|
|
908
|
+
.get(localSessionId));
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
beginTurnAttemptSubmission(attemptId: string, recordProvenance?: () => void): void {
|
|
912
|
+
this.db.transaction(() => {
|
|
913
|
+
const result = this.db.prepare(`
|
|
914
|
+
UPDATE turn_attempts SET submission_state = 'submission_unknown', updated_at = ?
|
|
915
|
+
WHERE attempt_id = ? AND submission_state = 'not_submitted'
|
|
916
|
+
AND status IN ('accepted', 'recovering') AND turn_id IS NULL
|
|
917
|
+
AND execution_generation = COALESCE((SELECT generation FROM task_execution_control
|
|
918
|
+
WHERE local_session_id = turn_attempts.local_session_id), 0)
|
|
919
|
+
`).run(new Date().toISOString(), attemptId);
|
|
920
|
+
if (result.changes !== 1) throw new Error("Prompt submission is not provably safe; automatic replay refused.");
|
|
921
|
+
recordProvenance?.();
|
|
922
|
+
})();
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
executionGeneration(localSessionId: string): number {
|
|
926
|
+
const row = this.db.prepare("SELECT generation FROM task_execution_control WHERE local_session_id = ?")
|
|
927
|
+
.get(localSessionId) as { generation: number } | undefined;
|
|
928
|
+
return row?.generation ?? 0;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
requestSessionExecutionStop(localSessionId: string): void {
|
|
932
|
+
this.db.transaction(() => {
|
|
933
|
+
this.db.prepare(`INSERT INTO task_execution_control (local_session_id, generation) VALUES (?, 1)
|
|
934
|
+
ON CONFLICT(local_session_id) DO UPDATE SET generation = generation + 1`).run(localSessionId);
|
|
935
|
+
this.db.prepare(`UPDATE turn_attempts SET status = 'cancelling', updated_at = ?
|
|
936
|
+
WHERE local_session_id = ? AND status IN ('accepted', 'running', 'recovering', 'unknown')
|
|
937
|
+
AND attempt_id NOT IN (SELECT prompt_id FROM queued_prompts)`)
|
|
938
|
+
.run(new Date().toISOString(), localSessionId);
|
|
939
|
+
})();
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
prepareUnstartedTurnAttemptRetry(attemptId: string): TurnAttemptRecord | undefined {
|
|
878
943
|
const existing = this.getTurnAttempt(attemptId);
|
|
879
|
-
if (!existing
|
|
944
|
+
if (!existing || existing.turnId || existing.submissionState !== "not_submitted"
|
|
945
|
+
|| !isIncompleteTurnAttemptStatus(existing.status)) return undefined;
|
|
880
946
|
this.updateTurnAttempt(attemptId, {
|
|
881
947
|
pendingTurnId: null,
|
|
882
|
-
|
|
883
|
-
recoveredFromTurnId: recoveredFromTurnId ?? existing.turnId ?? existing.recoveredFromTurnId ?? null,
|
|
884
|
-
recoveryCount: existing.recoveryCount + 1,
|
|
948
|
+
retryCount: existing.retryCount + 1,
|
|
885
949
|
status: "recovering",
|
|
886
950
|
});
|
|
887
951
|
return this.getTurnAttempt(attemptId);
|
|
888
952
|
}
|
|
889
953
|
|
|
890
|
-
|
|
954
|
+
prepareTurnAttemptRecovery(attemptId: string, recoveredFromTurnId?: string): TurnAttemptRecord | undefined {
|
|
891
955
|
const existing = this.getTurnAttempt(attemptId);
|
|
892
|
-
if (!existing || existing.turnId
|
|
893
|
-
return undefined;
|
|
894
|
-
}
|
|
956
|
+
if (!existing || existing.turnId || existing.submissionState !== "not_submitted"
|
|
957
|
+
|| !isIncompleteTurnAttemptStatus(existing.status)) return undefined;
|
|
895
958
|
this.updateTurnAttempt(attemptId, {
|
|
896
|
-
|
|
897
|
-
turnId: null,
|
|
898
|
-
recoveredFromTurnId: failedTurnId,
|
|
899
|
-
retryCount: existing.retryCount + 1,
|
|
900
|
-
status: "recovering",
|
|
959
|
+
recoveredFromTurnId, recoveryCount: existing.recoveryCount + 1, status: "recovering",
|
|
901
960
|
});
|
|
902
961
|
return this.getTurnAttempt(attemptId);
|
|
903
962
|
}
|
|
904
963
|
|
|
905
|
-
|
|
964
|
+
prepareTurnAttemptRetry(attemptId: string, failedTurnId: string, verifiedContinuation = false): TurnAttemptRecord | undefined {
|
|
906
965
|
const existing = this.getTurnAttempt(attemptId);
|
|
907
|
-
if (!
|
|
966
|
+
if (!verifiedContinuation || !existing || existing.turnId !== failedTurnId
|
|
967
|
+
|| !isIncompleteTurnAttemptStatus(existing.status)) return undefined;
|
|
908
968
|
this.updateTurnAttempt(attemptId, {
|
|
909
|
-
pendingTurnId: null,
|
|
910
|
-
retryCount: existing.retryCount + 1,
|
|
911
|
-
status: "recovering",
|
|
969
|
+
pendingTurnId: null, turnId: null, recoveredFromTurnId: failedTurnId,
|
|
970
|
+
retryCount: existing.retryCount + 1, status: "recovering", submissionState: "not_submitted",
|
|
912
971
|
});
|
|
913
972
|
return this.getTurnAttempt(attemptId);
|
|
914
973
|
}
|
|
915
974
|
|
|
975
|
+
acknowledgeUnknownTurnAttempts(localSessionId: string): void {
|
|
976
|
+
this.db.prepare("UPDATE turn_attempts SET status = 'interrupted' WHERE local_session_id = ? AND status = 'unknown'")
|
|
977
|
+
.run(localSessionId);
|
|
978
|
+
}
|
|
979
|
+
|
|
916
980
|
rebindPendingTurnMessages(localSessionId: string, sourceTurnId: string, targetTurnId: string): void {
|
|
917
981
|
const now = new Date().toISOString();
|
|
918
982
|
const rebind = this.db.transaction(() => {
|
|
@@ -2360,9 +2424,18 @@ export class StateStore {
|
|
|
2360
2424
|
const existing = new Set(
|
|
2361
2425
|
(this.db.pragma("table_info(turn_attempts)") as Array<{ name: string }>).map((column) => column.name),
|
|
2362
2426
|
);
|
|
2427
|
+
if (!existing.has("submission_state")) {
|
|
2428
|
+
this.db.transaction(() => {
|
|
2429
|
+
this.db.exec("ALTER TABLE turn_attempts ADD COLUMN submission_state TEXT NOT NULL DEFAULT 'submission_unknown'");
|
|
2430
|
+
this.db.exec("UPDATE turn_attempts SET submission_state = 'submitted' WHERE turn_id IS NOT NULL");
|
|
2431
|
+
})();
|
|
2432
|
+
}
|
|
2363
2433
|
if (!existing.has("retry_count")) {
|
|
2364
2434
|
this.db.exec("ALTER TABLE turn_attempts ADD COLUMN retry_count INTEGER NOT NULL DEFAULT 0");
|
|
2365
2435
|
}
|
|
2436
|
+
if (!existing.has("execution_generation")) {
|
|
2437
|
+
this.db.exec("ALTER TABLE turn_attempts ADD COLUMN execution_generation INTEGER NOT NULL DEFAULT 0");
|
|
2438
|
+
}
|
|
2366
2439
|
}
|
|
2367
2440
|
|
|
2368
2441
|
private ensureQueuedPromptColumns(): void {
|
|
@@ -2373,6 +2446,16 @@ export class StateStore {
|
|
|
2373
2446
|
this.db.exec("ALTER TABLE queued_prompts ADD COLUMN display_prompt TEXT");
|
|
2374
2447
|
}
|
|
2375
2448
|
}
|
|
2449
|
+
|
|
2450
|
+
private ensureRecentContextReceiptColumns(): void {
|
|
2451
|
+
const existing = new Set(
|
|
2452
|
+
(this.db.pragma("table_info(recent_context_receipts)") as Array<{ name: string }>).map((column) => column.name),
|
|
2453
|
+
);
|
|
2454
|
+
if (!existing.has("prompt_message_id")) {
|
|
2455
|
+
// Old receipts have no provable source Prompt, so they cannot be stop boundaries.
|
|
2456
|
+
this.db.exec("ALTER TABLE recent_context_receipts ADD COLUMN prompt_message_id TEXT NOT NULL DEFAULT ''");
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2376
2459
|
}
|
|
2377
2460
|
|
|
2378
2461
|
const MAX_TASK_TITLE_LENGTH = 120;
|
|
@@ -2500,6 +2583,8 @@ function mapTurnAttempt(row: TurnAttemptRow): TurnAttemptRecord {
|
|
|
2500
2583
|
recoveredFromTurnId: row.recovered_from_turn_id ?? undefined,
|
|
2501
2584
|
recoveryCount: row.recovery_count,
|
|
2502
2585
|
retryCount: row.retry_count,
|
|
2586
|
+
submissionState: row.submission_state,
|
|
2587
|
+
executionGeneration: row.execution_generation,
|
|
2503
2588
|
status: row.status,
|
|
2504
2589
|
createdAt: row.created_at,
|
|
2505
2590
|
updatedAt: row.updated_at,
|
package/src/state/migrations.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
export const migrations = [
|
|
2
|
+
`CREATE TABLE IF NOT EXISTS execution_provenance (
|
|
3
|
+
submission_id TEXT PRIMARY KEY, instruction_id TEXT NOT NULL, attempt_id TEXT NOT NULL,
|
|
4
|
+
local_session_id TEXT NOT NULL, turn_id TEXT, trigger TEXT NOT NULL, parent_source_turn_id TEXT,
|
|
5
|
+
scheduled_for INTEGER, submitted_at INTEGER NOT NULL, agent_name TEXT NOT NULL,
|
|
6
|
+
model_provider TEXT, model TEXT, reasoning_effort TEXT, permission_mode TEXT NOT NULL, cwd TEXT NOT NULL
|
|
7
|
+
);
|
|
8
|
+
CREATE INDEX IF NOT EXISTS idx_execution_provenance_attempt ON execution_provenance(attempt_id);
|
|
9
|
+
CREATE INDEX IF NOT EXISTS idx_execution_provenance_turn ON execution_provenance(turn_id, local_session_id);`,
|
|
10
|
+
`CREATE TABLE IF NOT EXISTS task_execution_control (
|
|
11
|
+
local_session_id TEXT PRIMARY KEY,
|
|
12
|
+
generation INTEGER NOT NULL DEFAULT 0
|
|
13
|
+
);`,
|
|
2
14
|
`
|
|
3
15
|
CREATE TABLE IF NOT EXISTS user_contexts (
|
|
4
16
|
context_key TEXT PRIMARY KEY,
|