@brainervirus/workit-core 1.0.9 → 1.0.10

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.
@@ -3,6 +3,7 @@ import * as fs from "node:fs";
3
3
  import { hostname } from "node:os";
4
4
  import path from "node:path";
5
5
  import * as z from "zod";
6
+ import { packageRoot } from "./package-root";
6
7
  import {
7
8
  acquireFileLockSync,
8
9
  type FileLockSyncHandle,
@@ -109,6 +110,43 @@ export type RecoveryCandidate = {
109
110
  };
110
111
 
111
112
  const now = (): Utc => new Date().toISOString().replace(/\.\d{3}Z$/, "Z");
113
+
114
+ let cachedRuntimeVersion: string | null = null;
115
+ /** Version of the package that hosts this runtime; recorded with state so a
116
+ * bug report can identify the writer version without reading raw .workit. */
117
+ export const runtimeVersion = (): string => {
118
+ if (cachedRuntimeVersion === null) {
119
+ try {
120
+ const pkg = JSON.parse(fs.readFileSync(path.join(packageRoot(), "package.json"), "utf8")) as {
121
+ version?: unknown;
122
+ };
123
+ cachedRuntimeVersion = typeof pkg.version === "string" && pkg.version ? pkg.version : "0.0.0";
124
+ } catch {
125
+ cachedRuntimeVersion = "0.0.0";
126
+ }
127
+ }
128
+ return cachedRuntimeVersion;
129
+ };
130
+
131
+ const stampNew = (record: { runtime?: { createdWith: string | null } }) => ({
132
+ createdWith: record.runtime?.createdWith ?? null,
133
+ updatedWith: runtimeVersion(),
134
+ });
135
+
136
+ const isNewerVersion = (candidate: string, current: string): boolean => {
137
+ const parts = (value: string) =>
138
+ value
139
+ .split("-")[0]
140
+ .split(".")
141
+ .map((item) => Number.parseInt(item, 10) || 0);
142
+ const [next, now] = [parts(candidate), parts(current)];
143
+ for (let index = 0; index < 3; index += 1) {
144
+ const left = next[index] ?? 0;
145
+ const right = now[index] ?? 0;
146
+ if (left !== right) return left > right;
147
+ }
148
+ return false;
149
+ };
112
150
  const jsonBytes = (value: unknown): string => `${canonicalJson(value)}\n`;
113
151
  const digestBytes = (value: string | Buffer): string =>
114
152
  createHash("sha256").update(value).digest("hex");
@@ -221,10 +259,14 @@ export class TaskStore {
221
259
  ? value.expectedWorkspaceRevision !== current.data.revision
222
260
  : value.expectedWorkspaceRevision !== null
223
261
  ) {
224
- return failure("revision_conflict", "workspace revision does not match", {
225
- expectedWorkspaceRevision: value.expectedWorkspaceRevision,
226
- actualWorkspaceRevision: current.data?.revision ?? null,
227
- });
262
+ return failure(
263
+ "revision_conflict",
264
+ "workspace revision does not match; omit expectedWorkspaceRevision to use the current record",
265
+ {
266
+ expectedWorkspaceRevision: value.expectedWorkspaceRevision,
267
+ actualWorkspaceRevision: current.data?.revision ?? null,
268
+ },
269
+ );
228
270
  }
229
271
  if (!value.provenance) return failure("invalid_input", "provenance is required");
230
272
  if (
@@ -236,12 +278,18 @@ export class TaskStore {
236
278
  if (current.data && !previousWorkspaceBytes)
237
279
  return failure("storage_error", "workspace snapshot disappeared during creation");
238
280
  const workspace: WorkspaceRecord = current.data
239
- ? { ...current.data, revision: newRevision(), root: this.root }
281
+ ? {
282
+ ...current.data,
283
+ revision: newRevision(),
284
+ root: this.root,
285
+ runtime: stampNew(current.data),
286
+ }
240
287
  : {
241
288
  schemaVersion: SCHEMA_VERSION,
242
289
  id: newId(),
243
290
  revision: newRevision(),
244
291
  root: this.root,
292
+ runtime: { createdWith: runtimeVersion(), updatedWith: runtimeVersion() },
245
293
  writer: null,
246
294
  };
247
295
  const timestamp = value.now ?? now();
@@ -263,6 +311,7 @@ export class TaskStore {
263
311
  status: "active",
264
312
  closure: null,
265
313
  progress: { summary: "", nextAction: null, blockers: [] },
314
+ runtime: { createdWith: runtimeVersion(), updatedWith: runtimeVersion() },
266
315
  assessments: [],
267
316
  policy: null,
268
317
  policyChanges: [],
@@ -312,21 +361,31 @@ export class TaskStore {
312
361
  ? input.expectedWorkspaceRevision !== current.data.revision
313
362
  : input.expectedWorkspaceRevision !== null
314
363
  )
315
- return failure("revision_conflict", "workspace revision does not match", {
316
- expectedWorkspaceRevision: input.expectedWorkspaceRevision,
317
- actualWorkspaceRevision: current.data?.revision ?? null,
318
- });
364
+ return failure(
365
+ "revision_conflict",
366
+ "workspace revision does not match; omit expectedWorkspaceRevision to use the current record",
367
+ {
368
+ expectedWorkspaceRevision: input.expectedWorkspaceRevision,
369
+ actualWorkspaceRevision: current.data?.revision ?? null,
370
+ },
371
+ );
319
372
  const previousWorkspaceBytes = current.data ? this.snapshotBytes(this.workspacePath) : null;
320
373
  if (current.data && !previousWorkspaceBytes)
321
374
  return failure("storage_error", "workspace snapshot disappeared during import");
322
375
  const timestamp = input.now ?? now();
323
376
  const workspace: WorkspaceRecord = current.data
324
- ? { ...current.data, revision: newRevision(), root: this.root }
377
+ ? {
378
+ ...current.data,
379
+ revision: newRevision(),
380
+ root: this.root,
381
+ runtime: stampNew(current.data),
382
+ }
325
383
  : {
326
384
  schemaVersion: SCHEMA_VERSION,
327
385
  id: input.workspaceId ?? newId(),
328
386
  revision: newRevision(),
329
387
  root: this.root,
388
+ runtime: { createdWith: runtimeVersion(), updatedWith: runtimeVersion() },
330
389
  writer: null,
331
390
  };
332
391
  const task = {
@@ -335,6 +394,10 @@ export class TaskStore {
335
394
  revision: newRevision(),
336
395
  createdAt: timestamp,
337
396
  updatedAt: timestamp,
397
+ runtime: {
398
+ createdWith: input.task.runtime?.createdWith ?? null,
399
+ updatedWith: runtimeVersion(),
400
+ },
338
401
  };
339
402
  const validTask = taskRecordSchema.safeParse(task);
340
403
  if (!validTask.success)
@@ -402,6 +465,7 @@ export class TaskStore {
402
465
  createdAt: current.data.createdAt,
403
466
  revision: context.revision,
404
467
  updatedAt: context.now,
468
+ runtime: stampNew(current.data),
405
469
  };
406
470
  const valid = taskRecordSchema.safeParse(record);
407
471
  if (!valid.success)
@@ -433,6 +497,7 @@ export class TaskStore {
433
497
  id: current.data.id,
434
498
  root: this.root,
435
499
  revision: context.revision,
500
+ runtime: stampNew(current.data),
436
501
  };
437
502
  const valid = workspaceRecordSchema.safeParse(record);
438
503
  if (!valid.success)
@@ -472,6 +537,7 @@ export class TaskStore {
472
537
  id: workspace.data.id,
473
538
  root: this.root,
474
539
  revision: workspaceContext.revision,
540
+ runtime: stampNew(workspace.data),
475
541
  });
476
542
  if (!nextWorkspace.success)
477
543
  return failure("invalid_input", "workspace mutation produced an invalid record");
@@ -508,6 +574,7 @@ export class TaskStore {
508
574
  createdAt: task.data.createdAt,
509
575
  revision: taskContext.revision,
510
576
  updatedAt: taskContext.now,
577
+ runtime: stampNew(task.data),
511
578
  });
512
579
  if (!nextTask.success) {
513
580
  this.markUncertain(nextWorkspace.data);
@@ -785,6 +852,7 @@ export class TaskStore {
785
852
  ...workspace,
786
853
  writer: { ...workspace.writer, state: "uncertain" as const },
787
854
  revision: newRevision(),
855
+ runtime: stampNew(workspace),
788
856
  };
789
857
  this.replaceSnapshot(this.workspacePath, value, workspace);
790
858
  }
@@ -1027,9 +1095,17 @@ export class TaskStore {
1027
1095
  if (isObject(value) && "schemaVersion" in value && value.schemaVersion !== SCHEMA_VERSION)
1028
1096
  return failure("unsupported_version", "unsupported snapshot schema version");
1029
1097
  const parsed = schema.safeParse(value);
1030
- return parsed.success
1031
- ? success(null, null, parsed.data)
1032
- : failure("recovery_required", "snapshot does not satisfy its schema");
1098
+ if (parsed.success) return success(null, null, parsed.data);
1099
+ const writerVersion =
1100
+ isObject(value) && isObject((value as { runtime?: unknown }).runtime)
1101
+ ? (value as { runtime: { updatedWith?: unknown } }).runtime.updatedWith
1102
+ : null;
1103
+ if (typeof writerVersion === "string" && isNewerVersion(writerVersion, runtimeVersion()))
1104
+ return failure(
1105
+ "recovery_required",
1106
+ `snapshot was written by workit ${writerVersion}; upgrade Workit before mutating this checkout`,
1107
+ );
1108
+ return failure("recovery_required", "snapshot does not satisfy its schema");
1033
1109
  }
1034
1110
 
1035
1111
  private processStart(pid: number): string | null {
@@ -1041,10 +1117,14 @@ export class TaskStore {
1041
1117
  }
1042
1118
 
1043
1119
  private conflict(expected: Revision, actual: Revision): Result<never> {
1044
- return failure("revision_conflict", "snapshot revision does not match", {
1045
- expectedRevision: expected,
1046
- actualRevision: actual,
1047
- });
1120
+ return failure(
1121
+ "revision_conflict",
1122
+ "snapshot revision does not match; omit expectedRevision to use the current record",
1123
+ {
1124
+ expectedRevision: expected,
1125
+ actualRevision: actual,
1126
+ },
1127
+ );
1048
1128
  }
1049
1129
 
1050
1130
  private taskPath(taskId: Id) {
@@ -93,6 +93,12 @@ const same = (left: unknown, right: unknown): boolean => {
93
93
  }
94
94
  };
95
95
 
96
+ /** The one shared definition of active or uncertain worker state used by
97
+ * close, revise, resume, and replacement-launch checks. */
98
+ export const UNCERTAIN_WORKER_STATES = ["dispatching", "running", "cancelling", "unknown"] as const;
99
+ export const isUncertainWorker = (state: string): boolean =>
100
+ (UNCERTAIN_WORKER_STATES as readonly string[]).includes(state);
101
+
96
102
  const callerSession = (caller: CallerContext): HostSession =>
97
103
  caller.session ?? { kind: "host", host: caller.host, handle: caller.actor };
98
104
 
package/src/core.ts CHANGED
@@ -22,6 +22,8 @@ export {
22
22
  decisionDigest,
23
23
  candidateDigest,
24
24
  requirementId,
25
+ BINDING_QUESTION_BUDGET,
26
+ workitBindingQuestionIssue,
25
27
  } from "./core/task-contract";
26
28
  export type {
27
29
  OperationFamily,
@@ -55,7 +57,7 @@ export type {
55
57
  ExportBundle,
56
58
  Result as ContractResult,
57
59
  } from "./core/task-contract";
58
- export { TaskStore } from "./core/task-store";
60
+ export { TaskStore, runtimeVersion } from "./core/task-store";
59
61
  export type { MetadataLock, ProcessEvidence, RecoveryInput } from "./core/task-store";
60
62
  export { compactTaskContext, reconcileResume } from "./core/task-context";
61
63
  export type {
@@ -85,8 +87,13 @@ export type {
85
87
  } from "./core/external-action";
86
88
  export {
87
89
  approvedExternalAction,
90
+ approvedPlanCommit,
91
+ planCommitDescriptor,
92
+ planCommitBinding,
93
+ commitMessageFromDescriptor,
88
94
  externalActionState,
89
95
  priorExternalAction,
96
+ priorResolvedDrift,
90
97
  externalActionRef,
91
98
  externalActionDescriptor,
92
99
  externalActionHelp,