minicode-ai 0.28.0 → 0.30.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.
- package/README.md +1 -1
- package/cli/args.ts +93 -70
- package/cli/bootstrap/presentation-bridge.ts +383 -0
- package/cli/commands/acp.ts +7 -1
- package/cli/commands/doctor.ts +7 -3
- package/cli/commands/exec.ts +6 -16
- package/cli/index.ts +3 -2
- package/cli/setup.ts +13 -380
- package/docs/TERMINAL_CONTRACT.md +5 -5
- package/docs/architecture.md +14 -29
- package/package.json +1 -1
- package/src/app/session.ts +25 -2
- package/src/app/tool-layer.ts +27 -0
- package/src/lib/atomic-write.ts +31 -59
- package/src/policy/jail.ts +6 -8
- package/src/providers/anthropic.ts +16 -5
- package/src/providers/responses.ts +37 -7
- package/src/providers/sse-bounds.ts +6 -4
- package/src/session/db/runs.ts +397 -0
- package/src/session/db/schema.ts +358 -0
- package/src/session/persistence.ts +64 -955
- package/src/task/db/schema.ts +325 -0
- package/src/task/db/types.ts +269 -0
- package/src/task/store.ts +74 -986
- package/src/ui/tui/transcript.ts +4 -0
- package/vendor/minicore/src/providers/openai-compat.ts +7 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite"
|
|
2
|
+
import { createHash } from "node:crypto"
|
|
3
|
+
import { LIMITS } from "../../constants.ts"
|
|
4
|
+
import { resolveLocalDbPath, withSqliteBusyRetrySync } from "../../lib/db-path.ts"
|
|
5
|
+
import { scrubSecrets } from "../../policy/scrub.ts"
|
|
6
|
+
import {
|
|
7
|
+
isTaskStatus,
|
|
8
|
+
type Task,
|
|
9
|
+
type TaskAcceptance,
|
|
10
|
+
TaskError,
|
|
11
|
+
type TaskEvidence,
|
|
12
|
+
type TaskProvenance,
|
|
13
|
+
type TaskStatus,
|
|
14
|
+
type TaskVerification,
|
|
15
|
+
} from "../model.ts"
|
|
16
|
+
import { TASK_DATA_VERSION, TASK_DB_FILENAME, type TaskRow } from "./types.ts"
|
|
17
|
+
|
|
18
|
+
export const SCHEMA_ATTEMPTS = 8
|
|
19
|
+
export const SCHEMA_BACKOFF_MS = 25
|
|
20
|
+
|
|
21
|
+
export function withBusyRetrySync<T>(fn: () => T, attempts = SCHEMA_ATTEMPTS): T | null {
|
|
22
|
+
return withSqliteBusyRetrySync(fn, { attempts, backoffMs: SCHEMA_BACKOFF_MS })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const DDL = `
|
|
26
|
+
CREATE TABLE IF NOT EXISTS tasks (
|
|
27
|
+
session_id TEXT NOT NULL,
|
|
28
|
+
task_id TEXT NOT NULL,
|
|
29
|
+
title TEXT NOT NULL,
|
|
30
|
+
status TEXT NOT NULL,
|
|
31
|
+
task_order INTEGER NOT NULL,
|
|
32
|
+
parent_id TEXT,
|
|
33
|
+
depends_on_json TEXT,
|
|
34
|
+
blocked_reason TEXT,
|
|
35
|
+
verification_json TEXT,
|
|
36
|
+
evidence_json TEXT,
|
|
37
|
+
acceptance_json TEXT,
|
|
38
|
+
provenance_json TEXT,
|
|
39
|
+
created_at TEXT NOT NULL,
|
|
40
|
+
updated_at TEXT NOT NULL,
|
|
41
|
+
revision INTEGER NOT NULL,
|
|
42
|
+
exec_generation INTEGER NOT NULL DEFAULT 0,
|
|
43
|
+
attempt_generation INTEGER,
|
|
44
|
+
execution_owner TEXT,
|
|
45
|
+
PRIMARY KEY (session_id, task_id)
|
|
46
|
+
);
|
|
47
|
+
CREATE INDEX IF NOT EXISTS idx_tasks_session_order ON tasks(session_id, task_order);
|
|
48
|
+
CREATE TABLE IF NOT EXISTS task_meta (
|
|
49
|
+
key TEXT PRIMARY KEY,
|
|
50
|
+
value TEXT NOT NULL
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
CREATE TABLE IF NOT EXISTS session_authority (
|
|
54
|
+
session_id TEXT NOT NULL,
|
|
55
|
+
incarnation INTEGER NOT NULL,
|
|
56
|
+
owner_token TEXT NOT NULL,
|
|
57
|
+
owner_pid INTEGER NOT NULL,
|
|
58
|
+
acquired_at INTEGER NOT NULL,
|
|
59
|
+
lease_expires_at INTEGER NOT NULL,
|
|
60
|
+
PRIMARY KEY (session_id, incarnation)
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
CREATE TABLE IF NOT EXISTS task_transfers (
|
|
64
|
+
transfer_id TEXT PRIMARY KEY,
|
|
65
|
+
task_id TEXT NOT NULL,
|
|
66
|
+
from_session_id TEXT NOT NULL,
|
|
67
|
+
from_incarnation INTEGER NOT NULL,
|
|
68
|
+
to_session_id TEXT NOT NULL,
|
|
69
|
+
to_incarnation INTEGER NOT NULL,
|
|
70
|
+
expected_revision INTEGER NOT NULL,
|
|
71
|
+
new_revision INTEGER NOT NULL,
|
|
72
|
+
status TEXT NOT NULL,
|
|
73
|
+
exec_generation INTEGER NOT NULL,
|
|
74
|
+
attempt_generation INTEGER,
|
|
75
|
+
execution_owner TEXT,
|
|
76
|
+
transferred_at TEXT NOT NULL
|
|
77
|
+
);
|
|
78
|
+
CREATE INDEX IF NOT EXISTS idx_task_transfers_from ON task_transfers(from_session_id, task_id);
|
|
79
|
+
CREATE INDEX IF NOT EXISTS idx_task_transfers_to ON task_transfers(to_session_id, task_id);
|
|
80
|
+
CREATE TABLE IF NOT EXISTS task_merges (
|
|
81
|
+
merge_id TEXT PRIMARY KEY,
|
|
82
|
+
session_id TEXT NOT NULL,
|
|
83
|
+
survivor_id TEXT NOT NULL,
|
|
84
|
+
duplicate_id TEXT NOT NULL,
|
|
85
|
+
evidence TEXT NOT NULL,
|
|
86
|
+
survivor_status TEXT NOT NULL,
|
|
87
|
+
duplicate_status TEXT NOT NULL,
|
|
88
|
+
duplicate_revision INTEGER NOT NULL,
|
|
89
|
+
merged_at TEXT NOT NULL
|
|
90
|
+
);
|
|
91
|
+
CREATE INDEX IF NOT EXISTS idx_task_merges_session ON task_merges(session_id, duplicate_id);
|
|
92
|
+
`
|
|
93
|
+
|
|
94
|
+
const DDL_STATEMENTS: readonly string[] = DDL.split(";")
|
|
95
|
+
.map((s) => s.trim())
|
|
96
|
+
.filter((s) => s.length > 0)
|
|
97
|
+
|
|
98
|
+
export const handles = new Map<string, Database>()
|
|
99
|
+
const initialized = new Set<string>()
|
|
100
|
+
export const txDepth = new Map<string, number>()
|
|
101
|
+
|
|
102
|
+
export function openTxPaths(): string[] {
|
|
103
|
+
return [...txDepth.entries()].filter(([, d]) => d > 0).map(([p]) => p)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function assertNoOpenTx(p: string, what: string): void {
|
|
107
|
+
if ((txDepth.get(p) ?? 0) > 0) {
|
|
108
|
+
throw new TaskError(
|
|
109
|
+
"TASK_TX_ACTIVE",
|
|
110
|
+
`cannot ${what} while a TaskStore transaction is open for this database`,
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function dbFile(cwd?: string): string {
|
|
116
|
+
return resolveLocalDbPath(TASK_DB_FILENAME, cwd)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* [PHASE 6K] Migrasi idempoten & non-destruktif kolom `exec_generation` dan `attempt_generation`
|
|
121
|
+
* memakai introspeksi `PRAGMA table_info(tasks)`.
|
|
122
|
+
*/
|
|
123
|
+
function migrateExecutionLineageSchema(db: Database): void {
|
|
124
|
+
const present = new Set(
|
|
125
|
+
(db.prepare("PRAGMA table_info(tasks)").all() as { name: string }[]).map((r) => r.name),
|
|
126
|
+
)
|
|
127
|
+
if (present.size === 0) return
|
|
128
|
+
|
|
129
|
+
const needsExec = !present.has("exec_generation")
|
|
130
|
+
const needsAttempt = !present.has("attempt_generation")
|
|
131
|
+
if (!needsExec && !needsAttempt) return
|
|
132
|
+
|
|
133
|
+
const run = db.transaction(() => {
|
|
134
|
+
if (needsExec) {
|
|
135
|
+
db.exec("ALTER TABLE tasks ADD COLUMN exec_generation INTEGER NOT NULL DEFAULT 0")
|
|
136
|
+
}
|
|
137
|
+
if (needsAttempt) {
|
|
138
|
+
db.exec("ALTER TABLE tasks ADD COLUMN attempt_generation INTEGER")
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
try {
|
|
142
|
+
withBusyRetrySync(() => {
|
|
143
|
+
run()
|
|
144
|
+
return true
|
|
145
|
+
})
|
|
146
|
+
} catch (e) {
|
|
147
|
+
throw new TaskError(
|
|
148
|
+
"TASK_MIGRATION_FAILURE",
|
|
149
|
+
`lineage migration failed: ${(e as Error).message}`,
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
const cur = db.prepare("SELECT value FROM task_meta WHERE key = ?").get("data_version") as
|
|
154
|
+
| { value: string }
|
|
155
|
+
| null
|
|
156
|
+
| undefined
|
|
157
|
+
const n = cur ? Number(cur.value) : 0
|
|
158
|
+
if (!Number.isFinite(n) || n < TASK_DATA_VERSION) {
|
|
159
|
+
db.prepare(
|
|
160
|
+
"INSERT INTO task_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value",
|
|
161
|
+
).run("data_version", String(TASK_DATA_VERSION))
|
|
162
|
+
}
|
|
163
|
+
} catch {}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* [PHASE 6P] Tambahkan kolom `execution_owner` secara aditif tanpa backfill spekulatif.
|
|
168
|
+
*/
|
|
169
|
+
function migrateExecutionOwnershipSchema(db: Database): void {
|
|
170
|
+
const present = new Set(
|
|
171
|
+
(db.prepare("PRAGMA table_info(tasks)").all() as { name: string }[]).map((r) => r.name),
|
|
172
|
+
)
|
|
173
|
+
if (present.size === 0 || present.has("execution_owner")) return
|
|
174
|
+
try {
|
|
175
|
+
withBusyRetrySync(() => {
|
|
176
|
+
db.exec("ALTER TABLE tasks ADD COLUMN execution_owner TEXT")
|
|
177
|
+
return true
|
|
178
|
+
})
|
|
179
|
+
} catch (e) {
|
|
180
|
+
throw new TaskError(
|
|
181
|
+
"TASK_MIGRATION_FAILURE",
|
|
182
|
+
`execution-ownership migration failed: ${(e as Error).message}`,
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function handle(cwd?: string): Database {
|
|
188
|
+
const p = dbFile(cwd)
|
|
189
|
+
const existing = handles.get(p)
|
|
190
|
+
if (existing) return existing
|
|
191
|
+
const opened = withBusyRetrySync(() => {
|
|
192
|
+
const d = new Database(p)
|
|
193
|
+
try {
|
|
194
|
+
d.exec(`PRAGMA busy_timeout=${LIMITS.SQLITE_BUSY_TIMEOUT_MS}`)
|
|
195
|
+
} catch {}
|
|
196
|
+
return d
|
|
197
|
+
})
|
|
198
|
+
if (!opened) {
|
|
199
|
+
throw new TaskError(
|
|
200
|
+
"TASK_DB_INIT_FAILURE",
|
|
201
|
+
`cannot open ${p} after ${SCHEMA_ATTEMPTS} busy-retry attempts`,
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
const db = opened
|
|
205
|
+
try {
|
|
206
|
+
if (!initialized.has(p)) {
|
|
207
|
+
const walOk = withBusyRetrySync(() => {
|
|
208
|
+
db.exec(
|
|
209
|
+
`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=${LIMITS.SQLITE_BUSY_TIMEOUT_MS}; PRAGMA synchronous=NORMAL; PRAGMA journal_size_limit=${LIMITS.SQLITE_WAL_SIZE_LIMIT_BYTES}; PRAGMA wal_autocheckpoint=${LIMITS.SQLITE_WAL_AUTOCHECKPOINT_PAGES};`,
|
|
210
|
+
)
|
|
211
|
+
return true
|
|
212
|
+
})
|
|
213
|
+
if (walOk === null) {
|
|
214
|
+
process.stderr.write(`[warn] taskstore: wal setup deferred, retry next open: ${p}\n`)
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
const { chmodSync } = require("node:fs") as typeof import("node:fs")
|
|
218
|
+
chmodSync(p, 0o600)
|
|
219
|
+
} catch {}
|
|
220
|
+
}
|
|
221
|
+
for (const stmt of DDL_STATEMENTS) {
|
|
222
|
+
const ok = withBusyRetrySync(() => {
|
|
223
|
+
db.exec(stmt)
|
|
224
|
+
return true
|
|
225
|
+
})
|
|
226
|
+
if (ok === null) throw new Error(`schema init did not complete: ${stmt.slice(0, 40)}`)
|
|
227
|
+
}
|
|
228
|
+
migrateExecutionLineageSchema(db)
|
|
229
|
+
migrateExecutionOwnershipSchema(db)
|
|
230
|
+
initialized.add(p)
|
|
231
|
+
} catch (e) {
|
|
232
|
+
try {
|
|
233
|
+
db.close()
|
|
234
|
+
} catch {}
|
|
235
|
+
handles.delete(p)
|
|
236
|
+
throw new TaskError("TASK_DB_INIT_FAILURE", `schema init failed: ${(e as Error).message}`)
|
|
237
|
+
}
|
|
238
|
+
handles.set(p, db)
|
|
239
|
+
return db
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function closeTaskStoreHandle(cwd?: string): void {
|
|
243
|
+
const p = dbFile(cwd)
|
|
244
|
+
assertNoOpenTx(p, "close the store")
|
|
245
|
+
const db = handles.get(p)
|
|
246
|
+
if (db) {
|
|
247
|
+
try {
|
|
248
|
+
db.close()
|
|
249
|
+
} catch {}
|
|
250
|
+
handles.delete(p)
|
|
251
|
+
initialized.delete(p)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function parseJson<T>(raw: string | null, fallback: T): T {
|
|
256
|
+
if (!raw) return fallback
|
|
257
|
+
try {
|
|
258
|
+
const v = JSON.parse(raw) as T
|
|
259
|
+
return v ?? fallback
|
|
260
|
+
} catch {
|
|
261
|
+
return fallback
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function parseStringArrayStrict(raw: string | null): string[] | null {
|
|
266
|
+
if (raw === null || raw.trim() === "") return []
|
|
267
|
+
try {
|
|
268
|
+
const v: unknown = JSON.parse(raw)
|
|
269
|
+
if (!Array.isArray(v)) return null
|
|
270
|
+
if (v.some((x) => typeof x !== "string")) return null
|
|
271
|
+
return v as string[]
|
|
272
|
+
} catch {
|
|
273
|
+
return null
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function rowToTask(r: TaskRow): Task {
|
|
278
|
+
return {
|
|
279
|
+
id: r.task_id,
|
|
280
|
+
sessionId: r.session_id,
|
|
281
|
+
title: r.title,
|
|
282
|
+
status: (isTaskStatus(r.status) ? r.status : "PENDING") as TaskStatus,
|
|
283
|
+
order: r.task_order,
|
|
284
|
+
parentId: r.parent_id,
|
|
285
|
+
dependsOn: parseJson<string[]>(r.depends_on_json, []),
|
|
286
|
+
blockedReason: r.blocked_reason,
|
|
287
|
+
verification: parseJson<TaskVerification | null>(r.verification_json, null),
|
|
288
|
+
evidence: parseJson<TaskEvidence[]>(r.evidence_json, []),
|
|
289
|
+
acceptance: parseJson<TaskAcceptance | null>(r.acceptance_json, null),
|
|
290
|
+
provenance: parseJson<TaskProvenance>(r.provenance_json, {
|
|
291
|
+
origin: "runtime",
|
|
292
|
+
source: "unknown",
|
|
293
|
+
}),
|
|
294
|
+
createdAt: r.created_at,
|
|
295
|
+
updatedAt: r.updated_at,
|
|
296
|
+
revision: r.revision,
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function j(value: unknown): string | null {
|
|
301
|
+
return value === null || value === undefined ? null : JSON.stringify(value)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Discard the handle cache. For hermetic tests only. */
|
|
305
|
+
export function resetTaskStoreHandles(): void {
|
|
306
|
+
const open = openTxPaths()
|
|
307
|
+
if (open.length > 0) {
|
|
308
|
+
throw new TaskError(
|
|
309
|
+
"TASK_TX_ACTIVE",
|
|
310
|
+
`cannot reset task store handles while ${open.length} transaction(s) are open`,
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
for (const db of handles.values()) {
|
|
314
|
+
try {
|
|
315
|
+
db.close()
|
|
316
|
+
} catch {}
|
|
317
|
+
}
|
|
318
|
+
handles.clear()
|
|
319
|
+
initialized.clear()
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Legacy-source fingerprint, used by migration so a re-import of identical content changes nothing. */
|
|
323
|
+
export function legacyDigest(raw: string): string {
|
|
324
|
+
return createHash("sha256").update(scrubSecrets(raw)).digest("hex").slice(0, 32)
|
|
325
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { scrubSecrets } from "../../policy/scrub.ts"
|
|
2
|
+
import type { CompletionEvidence } from "../../tools/todo.ts"
|
|
3
|
+
import type {
|
|
4
|
+
Task,
|
|
5
|
+
TaskAcceptance,
|
|
6
|
+
TaskEvidence,
|
|
7
|
+
TaskProvenance,
|
|
8
|
+
TaskStatus,
|
|
9
|
+
TaskVerification,
|
|
10
|
+
} from "../model.ts"
|
|
11
|
+
|
|
12
|
+
export const TASK_DB_FILENAME = "tasks.db"
|
|
13
|
+
export const MAX_DEPENDS_ON = 32
|
|
14
|
+
export const MAX_EVIDENCE = 32
|
|
15
|
+
export const TASK_DATA_VERSION = 1
|
|
16
|
+
|
|
17
|
+
export type TaskTransitionKind = "created" | "updated" | "claimed"
|
|
18
|
+
|
|
19
|
+
export interface TaskTransition {
|
|
20
|
+
kind: TaskTransitionKind
|
|
21
|
+
task: Task
|
|
22
|
+
previousStatus: TaskStatus | null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let taskTransitionSink: ((transition: TaskTransition) => void) | null = null
|
|
26
|
+
|
|
27
|
+
export function setTaskTransitionSink(sink: ((transition: TaskTransition) => void) | null): void {
|
|
28
|
+
taskTransitionSink = sink
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Emit SETELAH commit; kegagalan observer tak boleh menggagalkan transisi. */
|
|
32
|
+
export function emitTaskTransition(
|
|
33
|
+
kind: TaskTransitionKind,
|
|
34
|
+
task: Task,
|
|
35
|
+
previousStatus: TaskStatus | null,
|
|
36
|
+
): void {
|
|
37
|
+
try {
|
|
38
|
+
taskTransitionSink?.({ kind, task, previousStatus })
|
|
39
|
+
} catch {}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface TaskRow {
|
|
43
|
+
session_id: string
|
|
44
|
+
task_id: string
|
|
45
|
+
title: string
|
|
46
|
+
status: string
|
|
47
|
+
task_order: number
|
|
48
|
+
parent_id: string | null
|
|
49
|
+
depends_on_json: string | null
|
|
50
|
+
blocked_reason: string | null
|
|
51
|
+
verification_json: string | null
|
|
52
|
+
evidence_json: string | null
|
|
53
|
+
acceptance_json: string | null
|
|
54
|
+
provenance_json: string | null
|
|
55
|
+
created_at: string
|
|
56
|
+
updated_at: string
|
|
57
|
+
revision: number
|
|
58
|
+
exec_generation: number
|
|
59
|
+
attempt_generation: number | null
|
|
60
|
+
execution_owner: string | null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface ExecutionLineage {
|
|
64
|
+
readonly execGeneration: number
|
|
65
|
+
readonly attemptGeneration: number | null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type ExecutionOwner = "scheduler"
|
|
69
|
+
|
|
70
|
+
export interface ExecutionOwnership {
|
|
71
|
+
readonly executionOwner: ExecutionOwner | null
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type AttemptRecordOutcome = "RECORDED" | "TASK_GONE" | "SUPERSEDED" | "SESSION_SUPERSEDED"
|
|
75
|
+
|
|
76
|
+
export type LineageReconcileOutcome =
|
|
77
|
+
| "RECONCILED"
|
|
78
|
+
| "NOT_STRANDED"
|
|
79
|
+
| "REJECTED_STALE"
|
|
80
|
+
| "NOT_FOUND"
|
|
81
|
+
| "REFUSED_NO_OWNERSHIP"
|
|
82
|
+
|
|
83
|
+
export const ADOPTABLE_STATUSES: readonly TaskStatus[] = [
|
|
84
|
+
"PENDING",
|
|
85
|
+
"IN_PROGRESS",
|
|
86
|
+
"VERIFYING",
|
|
87
|
+
"BLOCKED",
|
|
88
|
+
"FAILED",
|
|
89
|
+
"RETRYING",
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
export const ADOPTABLE_STATUS_SET: ReadonlySet<TaskStatus> = new Set(ADOPTABLE_STATUSES)
|
|
93
|
+
export const CLAIMABLE_STATUSES: readonly TaskStatus[] = ["PENDING"]
|
|
94
|
+
|
|
95
|
+
export function sqlList(statuses: readonly TaskStatus[]): string {
|
|
96
|
+
return statuses.map((s) => `'${s}'`).join(", ")
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface TaskTransfer {
|
|
100
|
+
readonly transferId: string
|
|
101
|
+
readonly taskId: string
|
|
102
|
+
readonly fromSessionId: string
|
|
103
|
+
readonly fromIncarnation: number
|
|
104
|
+
readonly toSessionId: string
|
|
105
|
+
readonly toIncarnation: number
|
|
106
|
+
readonly expectedRevision: number
|
|
107
|
+
readonly newRevision: number
|
|
108
|
+
readonly status: string
|
|
109
|
+
readonly execGeneration: number
|
|
110
|
+
readonly attemptGeneration: number | null
|
|
111
|
+
readonly executionOwner: ExecutionOwner | null
|
|
112
|
+
readonly transferredAt: string
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface TaskTransferRequest {
|
|
116
|
+
readonly taskId: string
|
|
117
|
+
readonly fromSessionId: string
|
|
118
|
+
readonly fromIncarnation: number
|
|
119
|
+
readonly toSessionId: string
|
|
120
|
+
readonly toIncarnation: number
|
|
121
|
+
readonly expectedRevision: number
|
|
122
|
+
readonly sourceOwnerToken: string
|
|
123
|
+
readonly targetOwnerToken: string
|
|
124
|
+
readonly now?: number
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export type TaskTransferOutcome =
|
|
128
|
+
| "TRANSFERRED"
|
|
129
|
+
| "NOT_FOUND"
|
|
130
|
+
| "REFUSED_SELF_TRANSFER"
|
|
131
|
+
| "REJECTED_STALE_SOURCE_INCARNATION"
|
|
132
|
+
| "REJECTED_STALE_TARGET_INCARNATION"
|
|
133
|
+
| "REJECTED_STALE_REVISION"
|
|
134
|
+
| "REFUSED_NO_SOURCE_AUTHORITY"
|
|
135
|
+
| "REFUSED_NO_TARGET_AUTHORITY"
|
|
136
|
+
| "REFUSED_STATUS"
|
|
137
|
+
| "REFUSED_TARGET_ID_CONFLICT"
|
|
138
|
+
| "REFUSED_UNRESOLVED_RELATION"
|
|
139
|
+
| "REFUSED_CORRUPT_STATE"
|
|
140
|
+
|
|
141
|
+
export interface TaskTransferResult {
|
|
142
|
+
readonly outcome: TaskTransferOutcome
|
|
143
|
+
readonly task: Task | null
|
|
144
|
+
readonly transfer: TaskTransfer | null
|
|
145
|
+
readonly detail: string
|
|
146
|
+
readonly unresolvedRelations: readonly string[]
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface UnfinishedWorkItem {
|
|
150
|
+
readonly sessionId: string
|
|
151
|
+
readonly sessionIncarnation: number
|
|
152
|
+
readonly task: Task
|
|
153
|
+
readonly execGeneration: number
|
|
154
|
+
readonly attemptGeneration: number | null
|
|
155
|
+
readonly executionOwner: ExecutionOwner | null
|
|
156
|
+
readonly transferredFromSessionId: string | null
|
|
157
|
+
readonly transferredAt: string | null
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface TaskMerge {
|
|
161
|
+
readonly mergeId: string
|
|
162
|
+
readonly sessionId: string
|
|
163
|
+
readonly survivorId: string
|
|
164
|
+
readonly duplicateId: string
|
|
165
|
+
readonly evidence: string
|
|
166
|
+
readonly survivorStatus: string
|
|
167
|
+
readonly duplicateStatus: string
|
|
168
|
+
readonly duplicateRevision: number
|
|
169
|
+
readonly mergedAt: string
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface TaskDuplicateRequest {
|
|
173
|
+
readonly sessionId: string
|
|
174
|
+
readonly survivorId: string
|
|
175
|
+
readonly duplicateId: string
|
|
176
|
+
readonly evidence: string
|
|
177
|
+
readonly expectedRevision?: number
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type TaskDuplicateOutcome =
|
|
181
|
+
| "DECLARED"
|
|
182
|
+
| "NOT_FOUND"
|
|
183
|
+
| "REFUSED_SELF_MERGE"
|
|
184
|
+
| "REFUSED_NO_EVIDENCE"
|
|
185
|
+
| "REFUSED_TERMINAL_DUPLICATE"
|
|
186
|
+
| "REJECTED_STALE_REVISION"
|
|
187
|
+
|
|
188
|
+
export interface TaskDuplicateResult {
|
|
189
|
+
readonly outcome: TaskDuplicateOutcome
|
|
190
|
+
readonly duplicate: Task | null
|
|
191
|
+
readonly survivor: Task | null
|
|
192
|
+
readonly merge: TaskMerge | null
|
|
193
|
+
readonly detail: string
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface NewTaskInput {
|
|
197
|
+
title: string
|
|
198
|
+
status: TaskStatus
|
|
199
|
+
order: number
|
|
200
|
+
parentId?: string | null
|
|
201
|
+
dependsOn?: string[]
|
|
202
|
+
blockedReason?: string | null
|
|
203
|
+
verification?: TaskVerification | null
|
|
204
|
+
evidence?: TaskEvidence[]
|
|
205
|
+
acceptance?: TaskAcceptance | null
|
|
206
|
+
provenance: TaskProvenance
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface TaskPatch {
|
|
210
|
+
title?: string
|
|
211
|
+
status?: TaskStatus
|
|
212
|
+
order?: number
|
|
213
|
+
parentId?: string | null
|
|
214
|
+
dependsOn?: string[]
|
|
215
|
+
blockedReason?: string | null
|
|
216
|
+
verification?: TaskVerification | null
|
|
217
|
+
evidence?: TaskEvidence[]
|
|
218
|
+
acceptance?: TaskAcceptance | null
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface UpsertOptions {
|
|
222
|
+
evidence?: CompletionEvidence
|
|
223
|
+
expectedRevision?: number
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface PatchOptions extends UpsertOptions {
|
|
227
|
+
allowReopen?: boolean
|
|
228
|
+
authorityToken?: string
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export interface VerificationGateOutcome {
|
|
232
|
+
readonly ok: boolean
|
|
233
|
+
readonly verdict: "passed" | "failed" | "stale"
|
|
234
|
+
readonly exitCode?: number | null
|
|
235
|
+
readonly command?: string
|
|
236
|
+
readonly summary?: string
|
|
237
|
+
readonly integrityOk?: boolean
|
|
238
|
+
readonly timestamp?: string
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export type TaskAuthorityMode = "LEGACY" | "SCHEDULER"
|
|
242
|
+
|
|
243
|
+
export interface TaskStoreOptions {
|
|
244
|
+
authority?: TaskAuthorityMode
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function nowIso(): string {
|
|
248
|
+
return new Date().toISOString()
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function buildVerificationTaskEvidence(outcome: VerificationGateOutcome): TaskEvidence {
|
|
252
|
+
const at = outcome.timestamp ?? nowIso()
|
|
253
|
+
const payload = {
|
|
254
|
+
verdict: outcome.verdict,
|
|
255
|
+
ok: outcome.ok,
|
|
256
|
+
exitCode: outcome.exitCode ?? (outcome.ok ? 0 : 1),
|
|
257
|
+
...(outcome.command ? { command: scrubSecrets(outcome.command).slice(0, 200) } : {}),
|
|
258
|
+
summary: scrubSecrets(
|
|
259
|
+
outcome.summary ?? (outcome.ok ? "verification passed" : "verification failed"),
|
|
260
|
+
).slice(0, 500),
|
|
261
|
+
...(typeof outcome.integrityOk === "boolean" ? { integrityOk: outcome.integrityOk } : {}),
|
|
262
|
+
timestamp: at,
|
|
263
|
+
}
|
|
264
|
+
return {
|
|
265
|
+
kind: "verify",
|
|
266
|
+
detail: JSON.stringify(payload),
|
|
267
|
+
at,
|
|
268
|
+
}
|
|
269
|
+
}
|