@caupulican/pi-adaptative 0.94.0 → 0.94.1

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 (103) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/core/agent-paths.d.ts +2 -0
  3. package/dist/core/agent-paths.d.ts.map +1 -1
  4. package/dist/core/agent-paths.js +7 -0
  5. package/dist/core/agent-paths.js.map +1 -1
  6. package/dist/core/agent-session.d.ts.map +1 -1
  7. package/dist/core/agent-session.js +5 -1
  8. package/dist/core/agent-session.js.map +1 -1
  9. package/dist/core/background-lane-controller.d.ts +8 -0
  10. package/dist/core/background-lane-controller.d.ts.map +1 -1
  11. package/dist/core/background-lane-controller.js +75 -5
  12. package/dist/core/background-lane-controller.js.map +1 -1
  13. package/dist/core/background-tool-task-controller.d.ts +15 -0
  14. package/dist/core/background-tool-task-controller.d.ts.map +1 -1
  15. package/dist/core/background-tool-task-controller.js +122 -15
  16. package/dist/core/background-tool-task-controller.js.map +1 -1
  17. package/dist/core/context/long-term-memory-trigger.d.ts +3 -1
  18. package/dist/core/context/long-term-memory-trigger.d.ts.map +1 -1
  19. package/dist/core/context/long-term-memory-trigger.js +1 -1
  20. package/dist/core/context/long-term-memory-trigger.js.map +1 -1
  21. package/dist/core/context/okf-memory-provider.d.ts +3 -0
  22. package/dist/core/context/okf-memory-provider.d.ts.map +1 -1
  23. package/dist/core/context/okf-memory-provider.js +66 -31
  24. package/dist/core/context/okf-memory-provider.js.map +1 -1
  25. package/dist/core/context/okf-memory.d.ts +18 -1
  26. package/dist/core/context/okf-memory.d.ts.map +1 -1
  27. package/dist/core/context/okf-memory.js +48 -0
  28. package/dist/core/context/okf-memory.js.map +1 -1
  29. package/dist/core/delegation/worker-agent-control.d.ts +4 -0
  30. package/dist/core/delegation/worker-agent-control.d.ts.map +1 -1
  31. package/dist/core/delegation/worker-agent-control.js.map +1 -1
  32. package/dist/core/delegation/worker-execution-policy.d.ts.map +1 -1
  33. package/dist/core/delegation/worker-execution-policy.js +1 -1
  34. package/dist/core/delegation/worker-execution-policy.js.map +1 -1
  35. package/dist/core/delegation/worker-notification-coordinator.d.ts +10 -0
  36. package/dist/core/delegation/worker-notification-coordinator.d.ts.map +1 -1
  37. package/dist/core/delegation/worker-notification-coordinator.js +69 -3
  38. package/dist/core/delegation/worker-notification-coordinator.js.map +1 -1
  39. package/dist/core/foreground-terminal-handoff-controller.d.ts +17 -0
  40. package/dist/core/foreground-terminal-handoff-controller.d.ts.map +1 -1
  41. package/dist/core/foreground-terminal-handoff-controller.js +78 -10
  42. package/dist/core/foreground-terminal-handoff-controller.js.map +1 -1
  43. package/dist/core/learning/learning-audit.d.ts +5 -1
  44. package/dist/core/learning/learning-audit.d.ts.map +1 -1
  45. package/dist/core/learning/learning-audit.js +32 -1
  46. package/dist/core/learning/learning-audit.js.map +1 -1
  47. package/dist/core/learning/reflection-engine.d.ts +20 -0
  48. package/dist/core/learning/reflection-engine.d.ts.map +1 -1
  49. package/dist/core/learning/reflection-engine.js +58 -0
  50. package/dist/core/learning/reflection-engine.js.map +1 -1
  51. package/dist/core/memory/okf-project-memory-store.d.ts +28 -0
  52. package/dist/core/memory/okf-project-memory-store.d.ts.map +1 -0
  53. package/dist/core/memory/okf-project-memory-store.js +111 -0
  54. package/dist/core/memory/okf-project-memory-store.js.map +1 -0
  55. package/dist/core/memory/providers/file-store.d.ts +42 -1
  56. package/dist/core/memory/providers/file-store.d.ts.map +1 -1
  57. package/dist/core/memory/providers/file-store.js +274 -13
  58. package/dist/core/memory/providers/file-store.js.map +1 -1
  59. package/dist/core/memory-controller.d.ts +10 -1
  60. package/dist/core/memory-controller.d.ts.map +1 -1
  61. package/dist/core/memory-controller.js +72 -8
  62. package/dist/core/memory-controller.js.map +1 -1
  63. package/dist/core/provider-prompt-contracts.d.ts.map +1 -1
  64. package/dist/core/provider-prompt-contracts.js +9 -19
  65. package/dist/core/provider-prompt-contracts.js.map +1 -1
  66. package/dist/core/reflection-controller.d.ts +7 -0
  67. package/dist/core/reflection-controller.d.ts.map +1 -1
  68. package/dist/core/reflection-controller.js +94 -15
  69. package/dist/core/reflection-controller.js.map +1 -1
  70. package/dist/core/tools/delegate-status.d.ts +2 -0
  71. package/dist/core/tools/delegate-status.d.ts.map +1 -1
  72. package/dist/core/tools/delegate-status.js +4 -0
  73. package/dist/core/tools/delegate-status.js.map +1 -1
  74. package/dist/core/tools/delegate.d.ts +2 -0
  75. package/dist/core/tools/delegate.d.ts.map +1 -1
  76. package/dist/core/tools/delegate.js +83 -21
  77. package/dist/core/tools/delegate.js.map +1 -1
  78. package/dist/core/tools/tool-task.d.ts +1 -0
  79. package/dist/core/tools/tool-task.d.ts.map +1 -1
  80. package/dist/core/tools/tool-task.js +13 -8
  81. package/dist/core/tools/tool-task.js.map +1 -1
  82. package/dist/core/util/atomic-file.d.ts +2 -0
  83. package/dist/core/util/atomic-file.d.ts.map +1 -1
  84. package/dist/core/util/atomic-file.js +4 -0
  85. package/dist/core/util/atomic-file.js.map +1 -1
  86. package/dist/modes/interactive/auto-learn-controller.d.ts.map +1 -1
  87. package/dist/modes/interactive/auto-learn-controller.js +2 -0
  88. package/dist/modes/interactive/auto-learn-controller.js.map +1 -1
  89. package/dist/modes/interactive/components/activity-lane.d.ts.map +1 -1
  90. package/dist/modes/interactive/components/activity-lane.js +12 -23
  91. package/dist/modes/interactive/components/activity-lane.js.map +1 -1
  92. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
  93. package/dist/modes/interactive/components/custom-message.js +125 -1
  94. package/dist/modes/interactive/components/custom-message.js.map +1 -1
  95. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  96. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  97. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  98. package/examples/extensions/sandbox/package-lock.json +2 -2
  99. package/examples/extensions/sandbox/package.json +1 -1
  100. package/examples/extensions/with-deps/package-lock.json +2 -2
  101. package/examples/extensions/with-deps/package.json +1 -1
  102. package/npm-shrinkwrap.json +12 -12
  103. package/package.json +4 -4
@@ -7,6 +7,7 @@ declare const schema: Type.TObject<{
7
7
  }>;
8
8
  export interface ToolTaskDependencies {
9
9
  list(): BackgroundToolTaskRecord[];
10
+ observe(taskIds: readonly string[]): void;
10
11
  wait(taskId: string, signal?: AbortSignal): Promise<BackgroundToolTaskRecord>;
11
12
  cancel(taskId: string): boolean;
12
13
  }
@@ -1 +1 @@
1
- {"version":3,"file":"tool-task.d.ts","sourceRoot":"","sources":["../../../src/core/tools/tool-task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,uCAAuC,CAAC;AACtF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAK7D,QAAA,MAAM,MAAM;;;EAcX,CAAC;AAIF,MAAM,WAAW,oBAAoB;IACpC,IAAI,IAAI,wBAAwB,EAAE,CAAC;IACnC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;IAC9E,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC/B,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,CAAC;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC5C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAmBD,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,oBAAoB,GAAG,cAAc,CAAC,OAAO,MAAM,CAAC,CA4EtG"}
1
+ {"version":3,"file":"tool-task.d.ts","sourceRoot":"","sources":["../../../src/core/tools/tool-task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,uCAAuC,CAAC;AACtF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAK7D,QAAA,MAAM,MAAM;;;EAcX,CAAC;AAIF,MAAM,WAAW,oBAAoB;IACpC,IAAI,IAAI,wBAAwB,EAAE,CAAC;IACnC,OAAO,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;IAC1C,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;IAC9E,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC/B,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,CAAC;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC5C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAyBD,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,oBAAoB,GAAG,cAAc,CAAC,OAAO,MAAM,CAAC,CA8EtG"}
@@ -17,16 +17,19 @@ function validTaskId(value) {
17
17
  return undefined;
18
18
  return taskId;
19
19
  }
20
- function listText(records) {
20
+ function projectList(records) {
21
21
  if (records.length === 0)
22
- return "No background tool tasks in this session.";
22
+ return { included: [], text: "No background tool tasks in this session." };
23
23
  const included = records.slice(-MAX_LISTED_TASKS);
24
24
  const omitted = records.length - included.length;
25
- return [
26
- ...included.map((record) => `${record.taskId}: ${record.status} — ${record.summary}`),
27
- ...(omitted > 0 ? [`${omitted} older task(s) omitted from this bounded snapshot.`] : []),
28
- "Do not poll. Continue independent work, or call wait once only when a running task is a dependency.",
29
- ].join("\n");
25
+ return {
26
+ included,
27
+ text: [
28
+ ...included.map((record) => `${record.taskId}: ${record.status} — ${record.summary}`),
29
+ ...(omitted > 0 ? [`${omitted} older task(s) omitted from this bounded snapshot.`] : []),
30
+ "Do not poll. Continue independent work, or call wait once only when a running task is a dependency.",
31
+ ].join("\n"),
32
+ };
30
33
  }
31
34
  export function createToolTaskToolDefinition(deps) {
32
35
  return {
@@ -42,8 +45,10 @@ export function createToolTaskToolDefinition(deps) {
42
45
  async execute(_toolCallId, input, signal) {
43
46
  if (input.action === "list") {
44
47
  const records = deps.list();
48
+ const projection = projectList(records);
49
+ deps.observe(projection.included.map((record) => record.taskId));
45
50
  return {
46
- content: [{ type: "text", text: listText(records) }],
51
+ content: [{ type: "text", text: projection.text }],
47
52
  details: { kind: "list", count: records.length },
48
53
  };
49
54
  }
@@ -1 +1 @@
1
- {"version":3,"file":"tool-task.js","sourceRoot":"","sources":["../../../src/core/tools/tool-task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAI5C,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CACzB;IACC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC;QACnB,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC;QAChC,WAAW,EAAE,+DAA+D;KAC5E,CAAC;IACF,MAAM,EAAE,IAAI,CAAC,QAAQ,CACpB,IAAI,CAAC,MAAM,CAAC;QACX,SAAS,EAAE,iBAAiB;QAC5B,WAAW,EAAE,oCAAoC;KACjD,CAAC,CACF;CACD,EACD,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAC/B,CAAC;AAmBF,SAAS,W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,CAAC;gBAC3E,MAAM,IAAI,GACT,MAAM,CAAC,MAAM,KAAK,SAAS;oBAC1B,CAAC,CAAC,GAAG,MAAM,CAAC,OAAO,iGAAiG;oBACpH,CAAC,CAAC,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,OAAO,CAAC;gBACpC,OAAO;oBACN,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,CAAC;oBAC1C,OAAO,EAAE;wBACR,IAAI,EAAE,MAAe;wBACrB,MAAM;wBACN,MAAM,EAAE,MAAM,CAAC,MAAM;wBACrB,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;qBAC/D;oBACD,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,mBAA4B,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;iBAC9E,CAAC;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBACtE,OAAO;oBACN,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;oBAClD,OAAO,EAAE,EAAE,IAAI,EAAE,OAAgB,EAAE,MAAM,EAAE,MAAM,EAAE;oBACnD,OAAO,EAAE,IAAI;iBACb,CAAC;YACH,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { type Static, Type } from \"typebox\";\nimport type { BackgroundToolTaskRecord } from \"../background-tool-task-controller.ts\";\nimport type { ToolDefinition } from \"../extensions/types.ts\";\n\nconst MAX_TASK_ID_CHARS = 128;\nconst MAX_LISTED_TASKS = 32;\n\nconst schema = Type.Object(\n\t{\n\t\taction: Type.String({\n\t\t\tenum: [\"list\", \"wait\", \"cancel\"],\n\t\t\tdescription: \"List once, wait once for a dependency, or cancel. Never poll.\",\n\t\t}),\n\t\ttaskId: Type.Optional(\n\t\t\tType.String({\n\t\t\t\tmaxLength: MAX_TASK_ID_CHARS,\n\t\t\t\tdescription: \"Task id; required for wait/cancel.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\ntype Input = Static<typeof schema>;\n\nexport interface ToolTaskDependencies {\n\tlist(): BackgroundToolTaskRecord[];\n\twait(taskId: string, signal?: AbortSignal): Promise<BackgroundToolTaskRecord>;\n\tcancel(taskId: string): boolean;\n}\n\nexport interface ToolTaskDetails {\n\tkind: \"list\" | \"wait\" | \"cancel\" | \"error\";\n\tcount?: number;\n\ttaskId?: string;\n\tstatus?: BackgroundToolTaskRecord[\"status\"];\n\tartifactId?: string;\n\treason?: string;\n}\n\nfunction validTaskId(value: string | undefined): string | undefined {\n\tconst taskId = value?.trim();\n\tif (!taskId || taskId.length > MAX_TASK_ID_CHARS) return undefined;\n\treturn taskId;\n}\n\nfunction listText(records: readonly BackgroundToolTaskRecord[]): string {\n\tif (records.length === 0) return \"No background tool tasks in this session.\";\n\tconst included = records.slice(-MAX_LISTED_TASKS);\n\tconst omitted = records.length - included.length;\n\treturn [\n\t\t...included.map((record) => `${record.taskId}: ${record.status} — ${record.summary}`),\n\t\t...(omitted > 0 ? [`${omitted} older task(s) omitted from this bounded snapshot.`] : []),\n\t\t\"Do not poll. Continue independent work, or call wait once only when a running task is a dependency.\",\n\t].join(\"\\n\");\n}\n\nexport function createToolTaskToolDefinition(deps: ToolTaskDependencies): ToolDefinition<typeof schema> {\n\treturn {\n\t\tname: \"tool_task\",\n\t\tlabel: \"tool_task\",\n\t\tdescription:\n\t\t\t\"List, wait for, or cancel this session's background tool calls. Continue other work; wait is event-driven, never poll. Cite a completed taskId as goal evidence or task_steps evidence.\",\n\t\tpromptSnippet: \"Event-driven background tool control; wait once, never poll.\",\n\t\tpromptGuidelines: [\n\t\t\t\"Handoff is not completion. Need the result: wait once with taskId.\",\n\t\t\t\"Cite the taskId on the matching task_steps step and as goal add_evidence kind=tool uri.\",\n\t\t],\n\t\tparameters: schema,\n\t\tasync execute(_toolCallId, input: Input, signal) {\n\t\t\tif (input.action === \"list\") {\n\t\t\t\tconst records = deps.list();\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text: listText(records) }],\n\t\t\t\t\tdetails: { kind: \"list\" as const, count: records.length },\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst taskId = validTaskId(input.taskId);\n\t\t\tif (!taskId) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text: `${input.action} requires a valid taskId.` }],\n\t\t\t\t\tdetails: { kind: \"error\" as const, reason: \"invalid_task_id\" },\n\t\t\t\t\tisError: true,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (input.action === \"cancel\") {\n\t\t\t\tconst canceled = deps.cancel(taskId);\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\t\ttext: canceled\n\t\t\t\t\t\t\t\t? `Cancellation requested for ${taskId}.`\n\t\t\t\t\t\t\t\t: `No running background tool task named ${taskId}.`,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\tkind: \"cancel\" as const,\n\t\t\t\t\t\ttaskId,\n\t\t\t\t\t\t...(canceled ? {} : { reason: \"not_running\" }),\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst record = await deps.wait(taskId, signal);\n\t\t\t\tconst isError = record.status === \"failed\" || record.status === \"canceled\";\n\t\t\t\tconst text =\n\t\t\t\t\trecord.status === \"running\"\n\t\t\t\t\t\t? `${record.summary}\\nWait watchdog elapsed; continue independent work. The terminal handoff will wake the session.`\n\t\t\t\t\t\t: record.output || record.summary;\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text }],\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\tkind: \"wait\" as const,\n\t\t\t\t\t\ttaskId,\n\t\t\t\t\t\tstatus: record.status,\n\t\t\t\t\t\t...(record.artifactId ? { artifactId: record.artifactId } : {}),\n\t\t\t\t\t},\n\t\t\t\t\t...(isError ? { isError: true, errorKind: \"operation_outcome\" as const } : {}),\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text: reason }],\n\t\t\t\t\tdetails: { kind: \"error\" as const, taskId, reason },\n\t\t\t\t\tisError: true,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"tool-task.js","sourceRoot":"","sources":["../../../src/core/tools/tool-task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAI5C,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CACzB;IACC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC;QACnB,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC;QAChC,WAAW,EAAE,+DAA+D;KAC5E,CAAC;IACF,MAAM,EAAE,IAAI,CAAC,QAAQ,CACpB,IAAI,CAAC,MAAM,CAAC;QACX,SAAS,EAAE,iBAAiB;QAC5B,WAAW,EAAE,oCAAoC;KACjD,CAAC,CACF;CACD,EACD,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAC/B,CAAC;AAoBF,SAAS,WAAW,CAAC,KAAyB;IAC7C,MAAM,MAAM,GAAG,KAAK,EAAE,IAAI,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;QACF,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { type Static, Type } from \"typebox\";\nimport type { BackgroundToolTaskRecord } from \"../background-tool-task-controller.ts\";\nimport type { ToolDefinition } from \"../extensions/types.ts\";\n\nconst MAX_TASK_ID_CHARS = 128;\nconst MAX_LISTED_TASKS = 32;\n\nconst schema = Type.Object(\n\t{\n\t\taction: Type.String({\n\t\t\tenum: [\"list\", \"wait\", \"cancel\"],\n\t\t\tdescription: \"List once, wait once for a dependency, or cancel. Never poll.\",\n\t\t}),\n\t\ttaskId: Type.Optional(\n\t\t\tType.String({\n\t\t\t\tmaxLength: MAX_TASK_ID_CHARS,\n\t\t\t\tdescription: \"Task id; required for wait/cancel.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\ntype Input = Static<typeof schema>;\n\nexport interface ToolTaskDependencies {\n\tlist(): BackgroundToolTaskRecord[];\n\tobserve(taskIds: readonly string[]): void;\n\twait(taskId: string, signal?: AbortSignal): Promise<BackgroundToolTaskRecord>;\n\tcancel(taskId: string): boolean;\n}\n\nexport interface ToolTaskDetails {\n\tkind: \"list\" | \"wait\" | \"cancel\" | \"error\";\n\tcount?: number;\n\ttaskId?: string;\n\tstatus?: BackgroundToolTaskRecord[\"status\"];\n\tartifactId?: string;\n\treason?: string;\n}\n\nfunction validTaskId(value: string | undefined): string | undefined {\n\tconst taskId = value?.trim();\n\tif (!taskId || taskId.length > MAX_TASK_ID_CHARS) return undefined;\n\treturn taskId;\n}\n\nfunction projectList(records: readonly BackgroundToolTaskRecord[]): {\n\tincluded: readonly BackgroundToolTaskRecord[];\n\ttext: string;\n} {\n\tif (records.length === 0) return { included: [], text: \"No background tool tasks in this session.\" };\n\tconst included = records.slice(-MAX_LISTED_TASKS);\n\tconst omitted = records.length - included.length;\n\treturn {\n\t\tincluded,\n\t\ttext: [\n\t\t\t...included.map((record) => `${record.taskId}: ${record.status} — ${record.summary}`),\n\t\t\t...(omitted > 0 ? [`${omitted} older task(s) omitted from this bounded snapshot.`] : []),\n\t\t\t\"Do not poll. Continue independent work, or call wait once only when a running task is a dependency.\",\n\t\t].join(\"\\n\"),\n\t};\n}\n\nexport function createToolTaskToolDefinition(deps: ToolTaskDependencies): ToolDefinition<typeof schema> {\n\treturn {\n\t\tname: \"tool_task\",\n\t\tlabel: \"tool_task\",\n\t\tdescription:\n\t\t\t\"List, wait for, or cancel this session's background tool calls. Continue other work; wait is event-driven, never poll. Cite a completed taskId as goal evidence or task_steps evidence.\",\n\t\tpromptSnippet: \"Event-driven background tool control; wait once, never poll.\",\n\t\tpromptGuidelines: [\n\t\t\t\"Handoff is not completion. Need the result: wait once with taskId.\",\n\t\t\t\"Cite the taskId on the matching task_steps step and as goal add_evidence kind=tool uri.\",\n\t\t],\n\t\tparameters: schema,\n\t\tasync execute(_toolCallId, input: Input, signal) {\n\t\t\tif (input.action === \"list\") {\n\t\t\t\tconst records = deps.list();\n\t\t\t\tconst projection = projectList(records);\n\t\t\t\tdeps.observe(projection.included.map((record) => record.taskId));\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text: projection.text }],\n\t\t\t\t\tdetails: { kind: \"list\" as const, count: records.length },\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst taskId = validTaskId(input.taskId);\n\t\t\tif (!taskId) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text: `${input.action} requires a valid taskId.` }],\n\t\t\t\t\tdetails: { kind: \"error\" as const, reason: \"invalid_task_id\" },\n\t\t\t\t\tisError: true,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (input.action === \"cancel\") {\n\t\t\t\tconst canceled = deps.cancel(taskId);\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\t\ttext: canceled\n\t\t\t\t\t\t\t\t? `Cancellation requested for ${taskId}.`\n\t\t\t\t\t\t\t\t: `No running background tool task named ${taskId}.`,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\tkind: \"cancel\" as const,\n\t\t\t\t\t\ttaskId,\n\t\t\t\t\t\t...(canceled ? {} : { reason: \"not_running\" }),\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst record = await deps.wait(taskId, signal);\n\t\t\t\tconst isError = record.status === \"failed\" || record.status === \"canceled\";\n\t\t\t\tconst text =\n\t\t\t\t\trecord.status === \"running\"\n\t\t\t\t\t\t? `${record.summary}\\nWait watchdog elapsed; continue independent work. The terminal handoff will wake the session.`\n\t\t\t\t\t\t: record.output || record.summary;\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text }],\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\tkind: \"wait\" as const,\n\t\t\t\t\t\ttaskId,\n\t\t\t\t\t\tstatus: record.status,\n\t\t\t\t\t\t...(record.artifactId ? { artifactId: record.artifactId } : {}),\n\t\t\t\t\t},\n\t\t\t\t\t...(isError ? { isError: true, errorKind: \"operation_outcome\" as const } : {}),\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [{ type: \"text\" as const, text: reason }],\n\t\t\t\t\tdetails: { kind: \"error\" as const, taskId, reason },\n\t\t\t\t\tisError: true,\n\t\t\t\t};\n\t\t\t}\n\t\t},\n\t};\n}\n"]}
@@ -61,6 +61,8 @@ export interface AtomicFileWriteOptions {
61
61
  */
62
62
  fs?: FaultableFs;
63
63
  }
64
+ /** Narrow an unknown Node filesystem failure without unsafe casts at every storage owner. */
65
+ export declare function isMissingFileError(error: unknown): boolean;
64
66
  /**
65
67
  * Existing auth/settings/trust paths historically used 10 total attempts separated by 20ms. Keep
66
68
  * that low-latency user-facing policy explicit while routing the mechanism through this module.
@@ -1 +1 @@
1
-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,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,MAAM,IAAI,CAkBrG;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,CAAC,CAarG;AAED,0GAA0G;AAC1G,wBAAsB,YAAY,CAAC,CAAC,EACnC,QAAQ,EAAE,MAAM,EAChB,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACxB,OAAO,CAAC,EAAE,qBAAqB,GAC7B,OAAO,CAAC,CAAC,CAAC,CAqBZ;AA4CD,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,sBAAsB,GAAG,IAAI,CAa7G;AAED,wDAAwD;AACxD,wBAAsB,eAAe,CACpC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,sBAAsB,GAC9B,OAAO,CAAC,IAAI,CAAC,CAcf"}
@@ -82,6 +82,10 @@ async function renameWithRetry(tmpPath, filePath) {
82
82
  }
83
83
  }
84
84
  }
85
+ /** Narrow an unknown Node filesystem failure without unsafe casts at every storage owner. */
86
+ export function isMissingFileError(error) {
87
+ return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
88
+ }
85
89
  const DEFAULT_RETRIES = 10;
86
90
  const DEFAULT_REALPATH = false;
87
91
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"atomic-file.js","sourceRoot":"","sources":["../../../src/core/util/atomic-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,EAA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,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3F,mBAAmB,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE,CAAC,CAAC;QAC3C,OAAO,GAAG,IAAI,CAAC;IAChB,CAAC;YAAS,CAAC;QACV,IAAI,CAAC,OAAO;YAAE,uBAAuB,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC;AACF,CAAC;AAED,wDAAwD;AACxD,MAAM,CAAC,KAAK,UAAU,eAAe,CACpC,QAAgB,EAChB,OAAe,EACf,OAAgC;IAEhC,MAAM,yBAAyB,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;QACpD,MAAM,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/D,MAAM,OAAO,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;QACxC,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC;YACJ,yFAAyF;YACzF,MAAM,UAAU,CAAC,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;YACrG,MAAM,eAAe,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;YACzC,OAAO,GAAG,IAAI,CAAC;QAChB,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,OAAO;gBAAE,MAAM,mBAAmB,CAAC,OAAO,CAAC,CAAC;QAClD,CAAC;IACF,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Shared atomic-file helper: a single lock+tmp+rename implementation reused by every\n * on-disk store that does a read-modify-write against a JSON/text file. Modeled on the pattern\n * already proven in `memory/providers/file-store.ts` (proper-lockfile advisory lock + write-tmp-\n * then-rename); before this helper existed the pattern was copy-pasted per store, and most copies\n * were missing either the lock, the atomic write, or both.\n *\n * Two call shapes:\n * - `withFileLock(Sync)` — hold an exclusive advisory lock across an arbitrary read-modify-write\n * callback. The lock spans BOTH the read and the write, closing the classic RMW race where two\n * writers each read the old content before either writes back.\n * - `writeFileAtomic(Sync)` — write-tmp-then-rename. Used INSIDE a `withFileLock` callback (or\n * standalone, for a pure overwrite that has no read step to race).\n *\n * Sync and async variants are both exported. Most existing stores expose a synchronous public API\n * called from hot, non-async paths (e.g. a per-token-stream perf sample), so the sync variant lets\n * them gain locking without forcing an async ripple through their callers; the async variant is for\n * call sites that are already async.\n */\n\nimport { randomUUID } from \"node:crypto\";\nimport { promises as fsPromises, mkdirSync, unlinkSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nimport lockfile from \"proper-lockfile\";\nimport { type FaultableFs, nodeFs } from \"./faultable-fs.ts\";\n\n/**\n * Bounded rename retry budget for the win32-only transient-rename handling below (see\n * {@link isTransientRenameErrorOnWin32}). Backoff doubles each attempt starting at\n * {@link RENAME_RETRY_MIN_TIMEOUT_MS} and capped at {@link RENAME_RETRY_MAX_TIMEOUT_MS}, mirroring\n * the doubling-backoff shape used for lock acquisition above. With {@link RENAME_RETRY_ATTEMPTS}\n * extra attempts (10, 20, 40, 80, ... capped at 200) the worst-case total wait is ~1.03s\n * (10+20+40+80+160+200+200+200+200 ~= 1110ms across 9 gaps), which comfortably covers the\n * millisecond-scale window Defender/the Windows Search indexer hold a freshly-written file open\n * without FILE_SHARE_DELETE before releasing it.\n */\nconst RENAME_RETRY_ATTEMPTS = 9;\nconst RENAME_RETRY_MIN_TIMEOUT_MS = 10;\nconst RENAME_RETRY_MAX_TIMEOUT_MS = 200;\n\n/**\n * On win32, antivirus (e.g. Windows Defender's real-time scanner) and the Windows Search indexer\n * routinely open a freshly-written file for a brief scan without `FILE_SHARE_DELETE`, which makes a\n * rename over/of that file fail transiently with EPERM/EACCES/EBUSY for a few milliseconds. This is\n * a well-documented platform semantic, not a bug in our write path — it's exactly why the\n * `graceful-fs` package ships its own win32 rename-retry wrapper. POSIX platforms never exhibit this\n * transient (a POSIX rename either succeeds or fails for a real, non-transient reason), so retry is\n * gated strictly to win32 to keep POSIX behavior byte-identical to a bare rename.\n */\nfunction isTransientRenameErrorOnWin32(err: unknown): boolean {\n\tif (process.platform !== \"win32\") return false;\n\tif (typeof err !== \"object\" || err === null) return false;\n\tconst code = (err as { code?: string }).code;\n\treturn code === \"EPERM\" || code === \"EACCES\" || code === \"EBUSY\";\n}\n\n/** Sync counterpart of the rename-retry policy; see {@link isTransientRenameErrorOnWin32}. */\nfunction renameSyncWithRetry(tmpPath: string, filePath: string, fs: FaultableFs): void {\n\tfor (let attempt = 0; attempt <= RENAME_RETRY_ATTEMPTS; attempt++) {\n\t\ttry {\n\t\t\tfs.renameSync(tmpPath, filePath);\n\t\t\treturn;\n\t\t} catch (err) {\n\t\t\tif (!isTransientRenameErrorOnWin32(err) || attempt === RENAME_RETRY_ATTEMPTS) throw err;\n\t\t\tconst backoffMs = Math.min(RENAME_RETRY_MIN_TIMEOUT_MS * 2 ** attempt, RENAME_RETRY_MAX_TIMEOUT_MS);\n\t\t\tblockingSleepMs(backoffMs);\n\t\t}\n\t}\n}\n\n/** Async counterpart of the rename-retry policy; see {@link isTransientRenameErrorOnWin32}. */\nasync function renameWithRetry(tmpPath: string, filePath: string): Promise<void> {\n\tfor (let attempt = 0; attempt <= RENAME_RETRY_ATTEMPTS; attempt++) {\n\t\ttry {\n\t\t\tawait fsPromises.rename(tmpPath, filePath);\n\t\t\treturn;\n\t\t} catch (err) {\n\t\t\tif (!isTransientRenameErrorOnWin32(err) || attempt === RENAME_RETRY_ATTEMPTS) throw err;\n\t\t\tconst backoffMs = Math.min(RENAME_RETRY_MIN_TIMEOUT_MS * 2 ** attempt, RENAME_RETRY_MAX_TIMEOUT_MS);\n\t\t\tawait new Promise((resolve) => setTimeout(resolve, backoffMs));\n\t\t}\n\t}\n}\n\nexport interface AtomicFileLockOptions {\n\t/**\n\t * Bounded retry attempts while waiting for a lock already held elsewhere. Both variants use a\n\t * SHORT, capped backoff (see {@link RETRY_MIN_TIMEOUT_MS}/{@link RETRY_MAX_TIMEOUT_MS}) — these\n\t * stores' critical sections are sub-millisecond reads+writes of small JSON/text files, so\n\t * contention should clear in milliseconds, not the multi-second-to-31-second worst case\n\t * proper-lockfile's OWN default backoff produces for a bare numeric `retries` (its default\n\t * `minTimeout` is 1000ms with factor 2 — see node_modules/retry/lib/retry.js). Passing a bare\n\t * number straight through would turn brief contention into a multi-second stall on a hot path\n\t * (e.g. a per-token-stream perf sample), so both variants instead build an explicit short-backoff\n\t * `retry` options object.\n\t * - Async (`withFileLock`): forwarded as `{retries, minTimeout, maxTimeout}` to proper-lockfile.\n\t * - Sync (`withFileLockSync`): proper-lockfile's sync API REJECTS `retries > 0` outright (it\n\t * requires the whole acquire flow to be synchronous — see proper-lockfile/lib/adapter.js\n\t * `toSyncOptions`, which throws `ESYNC`). So the sync path implements its own bounded retry\n\t * around single `lockfile.lockSync` attempts, blocking briefly between them (Atomics.wait) —\n\t * callers are already fully synchronous fs code, so a short blocking wait on contention matches\n\t * the existing execution model rather than introducing a new one.\n\t */\n\tretries?: number;\n\t/** Initial delay between sync/async acquisition attempts; defaults to 25ms. */\n\tminRetryDelayMs?: number;\n\t/** Maximum delay between sync/async acquisition attempts; defaults to 500ms. */\n\tmaxRetryDelayMs?: number;\n\t/** Retry-delay multiplier; defaults to 2. Set max=min for a fixed delay. */\n\tretryFactor?: number;\n\t/** Resolve symlinks before locking (proper-lockfile `realpath`); false matches file-store.ts. */\n\trealpath?: boolean;\n\t/** Lock staleness window in ms (proper-lockfile `stale`); omitted = proper-lockfile's own default. */\n\tstale?: number;\n\t/** Explicit proper-lockfile directory path; defaults to `${filePath}.lock`. */\n\tlockfilePath?: string;\n}\n\nexport interface AtomicFileWriteOptions {\n\t/** POSIX permission bits applied to the temporary file and inherited by the renamed destination. */\n\tmode?: number;\n\t/**\n\t * Injection seam for the mutating fs primitives this write issues (`mkdirSync`, `writeFileSync`,\n\t * `renameSync`). Defaults to real `node:fs` — omitting this option is a zero-behavior-change no-op.\n\t * Only the destructive-testing harness passes a fault-injecting implementation.\n\t */\n\tfs?: FaultableFs;\n}\n\nconst DEFAULT_RETRIES = 10;\nconst DEFAULT_REALPATH = false;\n/**\n * Capped doubling backoff shared by both variants — see {@link AtomicFileLockOptions.retries}.\n * With the current defaults (10 retries, 25ms floor, 500ms cap) the total worst-case contention\n * budget is ~3.3s (25+50+100+200+400+500+500+500+500+500), comfortably over the >= 2s floor this\n * needs to survive a loaded CI runner (e.g. the Windows CI mkdir/rmdir lock-directory churn under\n * two-real-OS-thread contention).\n */\nconst RETRY_MIN_TIMEOUT_MS = 25;\nconst RETRY_MAX_TIMEOUT_MS = 500;\n\n/**\n * Existing auth/settings/trust paths historically used 10 total attempts separated by 20ms. Keep\n * that low-latency user-facing policy explicit while routing the mechanism through this module.\n */\nexport const LOW_LATENCY_FILE_LOCK_OPTIONS: Readonly<AtomicFileLockOptions> = Object.freeze({\n\tretries: 9,\n\tminRetryDelayMs: 20,\n\tmaxRetryDelayMs: 20,\n});\n\nfunction lockDirectory(filePath: string, lockfilePath?: string): string {\n\treturn dirname(lockfilePath ?? filePath);\n}\n\nfunction ensureLockDirSync(filePath: string, lockfilePath?: string): void {\n\tmkdirSync(lockDirectory(filePath, lockfilePath), { recursive: true });\n}\n\nasync function ensureLockDir(filePath: string, lockfilePath?: string): Promise<void> {\n\tawait fsPromises.mkdir(lockDirectory(filePath, lockfilePath), { recursive: true });\n}\n\nfunction isLockedError(err: unknown): boolean {\n\tif (typeof err !== \"object\" || err === null) return false;\n\tconst code = (err as { code?: string }).code;\n\tif (code === \"ELOCKED\") return true;\n\t// On win32, mkdir-ing the lock directory can transiently surface EPERM (rather than the\n\t// expected EEXIST) when a previous incarnation of that directory is concurrently being\n\t// rmdir'd by the racing releaser — the mkdir lands mid-teardown and the OS reports \"operation\n\t// not permitted\" instead of \"already exists\". This is contention, not a real permissions\n\t// failure, so treat it identically to ELOCKED: retry with the existing backoff rather than\n\t// letting it escape as fatal. POSIX platforms don't exhibit this transient and keep surfacing\n\t// real EPERM (e.g. an actually unwritable directory) as fatal.\n\tif (code === \"EPERM\" && process.platform === \"win32\") return true;\n\treturn false;\n}\n\n/** Block the calling thread for `ms` without spinning the CPU (Atomics.wait on a private buffer). */\nfunction blockingSleepMs(ms: number): void {\n\tAtomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);\n}\n\ntype PendingPathOperations = Map<string, Promise<void>>;\n\nconst pendingAsyncFileLockTails: PendingPathOperations = new Map();\nconst pendingAtomicWriteTails: PendingPathOperations = new Map();\n\nfunction pathOperationKey(filePath: string): string {\n\tconst absolutePath = resolve(filePath);\n\treturn process.platform === \"win32\" ? absolutePath.toLowerCase() : absolutePath;\n}\n\n/**\n * Queue only operations that target the same platform path. This prevents same-process callers\n * from entering synchronized OS retry storms without imposing a process-wide or cross-tenant\n * bottleneck. The tail always resolves and self-removes, including after a failed operation.\n */\nasync function withSerializedPathOperation<T>(\n\ttails: PendingPathOperations,\n\tfilePath: string,\n\tfn: () => Promise<T>,\n): Promise<T> {\n\tconst key = pathOperationKey(filePath);\n\tconst preceding = tails.get(key) ?? Promise.resolve();\n\tlet releaseTail: (() => void) | undefined;\n\tconst tail = new Promise<void>((resolveTail) => {\n\t\treleaseTail = resolveTail;\n\t});\n\ttails.set(key, tail);\n\n\tawait preceding;\n\ttry {\n\t\treturn await fn();\n\t} finally {\n\t\treleaseTail?.();\n\t\tif (tails.get(key) === tail) tails.delete(key);\n\t}\n}\n\n/**\n * Acquire a proper-lockfile sync lock with bounded retry on `ELOCKED` (proper-lockfile's sync API\n * itself forbids `retries > 0`; see {@link AtomicFileLockOptions.retries}).\n *\n * Backoff doubles each attempt (mirroring the `retry` module's own `factor: 2` default that the\n * async path gets for free from proper-lockfile), starting at {@link RETRY_MIN_TIMEOUT_MS} and\n * capped at {@link RETRY_MAX_TIMEOUT_MS}.\n *\n * This is budget PARITY between two implementations of the same lock-acquisition contract, not\n * flake-masking: `options.retries` (a count of RETRIES, i.e. attempts after the first) means the\n * same thing on both paths, so both must make `retries + 1` total acquisition attempts with the\n * same per-gap backoff schedule. The async path gets `retries + 1` attempts for free from the\n * `retry` module (it retries `options.retries` times after its initial attempt); the sync path\n * used to loop exactly `attempts` times total (one fewer gap, and one fewer attempt than the\n * async path for the same `retries` value), which under-budgeted it relative to its async sibling\n * and surfaced as spurious ELOCKED failures under real contention (e.g. two OS threads hammering\n * the same file, or mkdir/rmdir lock-directory churn on a loaded Windows CI runner).\n */\nfunction retryCount(options: AtomicFileLockOptions): number {\n\tconst configured = options.retries ?? DEFAULT_RETRIES;\n\treturn Number.isFinite(configured) ? Math.max(0, Math.floor(configured)) : DEFAULT_RETRIES;\n}\n\nfunction retryDelayMs(options: AtomicFileLockOptions, attempt: number): number {\n\tconst configuredMin = options.minRetryDelayMs ?? RETRY_MIN_TIMEOUT_MS;\n\tconst minDelay = Number.isFinite(configuredMin) ? Math.max(0, configuredMin) : RETRY_MIN_TIMEOUT_MS;\n\tconst configuredMax = options.maxRetryDelayMs ?? RETRY_MAX_TIMEOUT_MS;\n\tconst maxDelay = Number.isFinite(configuredMax) ? Math.max(minDelay, configuredMax) : RETRY_MAX_TIMEOUT_MS;\n\tconst configuredFactor = options.retryFactor ?? 2;\n\tconst factor = Number.isFinite(configuredFactor) ? Math.max(1, configuredFactor) : 2;\n\treturn Math.min(minDelay * factor ** (attempt - 1), maxDelay);\n}\n\n/**\n * Acquire a synchronous advisory file lock and return its release function. This is the shared\n * mechanism for coordinators that must hold several locks in a deterministic order before entering\n * one critical section; callers retain ownership of release ordering and release-error semantics.\n */\nexport function acquireFileLockSync(filePath: string, options: AtomicFileLockOptions = {}): () => void {\n\tensureLockDirSync(filePath, options.lockfilePath);\n\tconst attempts = retryCount(options) + 1;\n\tconst lockOptions = {\n\t\tlockfilePath: options.lockfilePath,\n\t\trealpath: options.realpath ?? DEFAULT_REALPATH,\n\t\tstale: options.stale,\n\t};\n\tfor (let attempt = 1; attempt <= attempts; attempt++) {\n\t\ttry {\n\t\t\treturn lockfile.lockSync(filePath, lockOptions);\n\t\t} catch (err) {\n\t\t\tif (!isLockedError(err) || attempt === attempts) throw err;\n\t\t\tblockingSleepMs(retryDelayMs(options, attempt));\n\t\t}\n\t}\n\t// Unreachable (the loop always returns or throws), but keeps the function's return type honest.\n\tthrow new Error(`Failed to acquire lock for ${filePath}`);\n}\n\n/**\n * Hold an exclusive advisory lock on `filePath` for the duration of `fn` (sync). Always releases,\n * including when `fn` throws.\n */\nexport function withFileLockSync<T>(filePath: string, fn: () => T, options?: AtomicFileLockOptions): T {\n\tconst release = acquireFileLockSync(filePath, options);\n\ttry {\n\t\treturn fn();\n\t} finally {\n\t\t// A lock-cleanup failure must never mask fn()'s result (or replace fn()'s own thrown error) —\n\t\t// by this point fn() has already durably committed or failed on its own terms.\n\t\ttry {\n\t\t\trelease();\n\t\t} catch {\n\t\t\t// best-effort cleanup; a stale lock self-expires via proper-lockfile's `stale` window\n\t\t}\n\t}\n}\n\n/** Async counterpart of {@link withFileLockSync}. Always releases, including when `fn` throws/rejects. */\nexport async function withFileLock<T>(\n\tfilePath: string,\n\tfn: () => Promise<T> | T,\n\toptions?: AtomicFileLockOptions,\n): Promise<T> {\n\treturn withSerializedPathOperation(pendingAsyncFileLockTails, options?.lockfilePath ?? filePath, async () => {\n\t\tawait ensureLockDir(filePath, options?.lockfilePath);\n\t\tconst release = await lockfile.lock(filePath, {\n\t\t\tlockfilePath: options?.lockfilePath,\n\t\t\trealpath: options?.realpath ?? DEFAULT_REALPATH,\n\t\t\tretries: {\n\t\t\t\tretries: retryCount(options ?? {}),\n\t\t\t\tfactor: options?.retryFactor ?? 2,\n\t\t\t\tminTimeout: options?.minRetryDelayMs ?? RETRY_MIN_TIMEOUT_MS,\n\t\t\t\tmaxTimeout: options?.maxRetryDelayMs ?? RETRY_MAX_TIMEOUT_MS,\n\t\t\t},\n\t\t\tstale: options?.stale,\n\t\t});\n\t\ttry {\n\t\t\treturn await fn();\n\t\t} finally {\n\t\t\t// See {@link withFileLockSync} — cleanup failures must not mask fn()'s outcome.\n\t\t\tawait release().catch(() => {});\n\t\t}\n\t});\n}\n\n/**\n * Write `content` to `filePath` via write-tmp-then-rename (sync); the rename is atomic on the same\n * filesystem, so a concurrent reader never observes a partially-written file. Does NOT itself lock —\n * call from inside {@link withFileLockSync} when the write follows a read that must not race another\n * writer's read+write; call standalone for an unconditional overwrite with no read step.\n */\nfunction temporaryPath(filePath: string): string {\n\treturn `${filePath}.${process.pid}.${randomUUID()}.tmp`;\n}\n\n/**\n * Async standalone overwrites can legitimately target the same file at once. Unique temporary\n * names prevent writers from corrupting each other's staging files, but Windows still serializes\n * replacement renames internally. Letting every writer retry in lockstep creates a thundering herd\n * that can exhaust the bounded Defender/indexer retry budget even though no external process owns\n * the destination. Keep one FIFO tail per destination inside this process so each destination has\n * one writer at a time; writes to different destinations remain fully parallel. The tail is always\n * resolved and removed, including after a failed write, so one failure cannot poison later writes\n * or retain tenant paths indefinitely. File-lock queues are separate from atomic-write queues so a\n * write inside a held advisory lock never waits on itself.\n */\nasync function withSerializedAtomicWrite<T>(filePath: string, fn: () => Promise<T>): Promise<T> {\n\treturn withSerializedPathOperation(pendingAtomicWriteTails, filePath, fn);\n}\n\n/** Remove one invocation's temporary path without masking the original write/rename failure. */\nfunction removeTemporaryPathSync(tmpPath: string): void {\n\ttry {\n\t\tunlinkSync(tmpPath);\n\t} catch {\n\t\t// The rename may have consumed the path, or another failure may have prevented its creation.\n\t}\n}\n\nasync function removeTemporaryPath(tmpPath: string): Promise<void> {\n\ttry {\n\t\tawait fsPromises.unlink(tmpPath);\n\t} catch {\n\t\t// The rename may have consumed the path, or another failure may have prevented its creation.\n\t}\n}\n\nexport function writeFileAtomicSync(filePath: string, content: string, options?: AtomicFileWriteOptions): void {\n\tconst fs = options?.fs ?? nodeFs;\n\tfs.mkdirSync(dirname(filePath), { recursive: true });\n\tconst tmpPath = temporaryPath(filePath);\n\tlet renamed = false;\n\ttry {\n\t\t// `wx` makes a nonce collision harmless: never truncate another writer's temporary file.\n\t\tfs.writeFileSync(tmpPath, content, { encoding: \"utf-8\", flag: \"wx\", mode: options?.mode });\n\t\trenameSyncWithRetry(tmpPath, filePath, fs);\n\t\trenamed = true;\n\t} finally {\n\t\tif (!renamed) removeTemporaryPathSync(tmpPath);\n\t}\n}\n\n/** Async counterpart of {@link writeFileAtomicSync}. */\nexport async function writeFileAtomic(\n\tfilePath: string,\n\tcontent: string,\n\toptions?: AtomicFileWriteOptions,\n): Promise<void> {\n\tawait withSerializedAtomicWrite(filePath, async () => {\n\t\tawait fsPromises.mkdir(dirname(filePath), { recursive: true });\n\t\tconst tmpPath = temporaryPath(filePath);\n\t\tlet renamed = false;\n\t\ttry {\n\t\t\t// `wx` makes a nonce collision harmless: never truncate another writer's temporary file.\n\t\t\tawait fsPromises.writeFile(tmpPath, content, { encoding: \"utf-8\", flag: \"wx\", mode: options?.mode });\n\t\t\tawait renameWithRetry(tmpPath, filePath);\n\t\t\trenamed = true;\n\t\t} finally {\n\t\t\tif (!renamed) await removeTemporaryPath(tmpPath);\n\t\t}\n\t});\n}\n"]}
1
+ {"version":3,"file":"atomic-file.js","sourceRoot":"","sources":["../../../src/core/util/atomic-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,QAAQ,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAoB,MAAM,EAAE,MAAM,mBAAmB,CA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disk store that does a read-modify-write against a JSON/text file. Modeled on the pattern\n * already proven in `memory/providers/file-store.ts` (proper-lockfile advisory lock + write-tmp-\n * then-rename); before this helper existed the pattern was copy-pasted per store, and most copies\n * were missing either the lock, the atomic write, or both.\n *\n * Two call shapes:\n * - `withFileLock(Sync)` — hold an exclusive advisory lock across an arbitrary read-modify-write\n * callback. The lock spans BOTH the read and the write, closing the classic RMW race where two\n * writers each read the old content before either writes back.\n * - `writeFileAtomic(Sync)` — write-tmp-then-rename. Used INSIDE a `withFileLock` callback (or\n * standalone, for a pure overwrite that has no read step to race).\n *\n * Sync and async variants are both exported. Most existing stores expose a synchronous public API\n * called from hot, non-async paths (e.g. a per-token-stream perf sample), so the sync variant lets\n * them gain locking without forcing an async ripple through their callers; the async variant is for\n * call sites that are already async.\n */\n\nimport { randomUUID } from \"node:crypto\";\nimport { promises as fsPromises, mkdirSync, unlinkSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nimport lockfile from \"proper-lockfile\";\nimport { type FaultableFs, nodeFs } from \"./faultable-fs.ts\";\n\n/**\n * Bounded rename retry budget for the win32-only transient-rename handling below (see\n * {@link isTransientRenameErrorOnWin32}). Backoff doubles each attempt starting at\n * {@link RENAME_RETRY_MIN_TIMEOUT_MS} and capped at {@link RENAME_RETRY_MAX_TIMEOUT_MS}, mirroring\n * the doubling-backoff shape used for lock acquisition above. With {@link RENAME_RETRY_ATTEMPTS}\n * extra attempts (10, 20, 40, 80, ... capped at 200) the worst-case total wait is ~1.03s\n * (10+20+40+80+160+200+200+200+200 ~= 1110ms across 9 gaps), which comfortably covers the\n * millisecond-scale window Defender/the Windows Search indexer hold a freshly-written file open\n * without FILE_SHARE_DELETE before releasing it.\n */\nconst RENAME_RETRY_ATTEMPTS = 9;\nconst RENAME_RETRY_MIN_TIMEOUT_MS = 10;\nconst RENAME_RETRY_MAX_TIMEOUT_MS = 200;\n\n/**\n * On win32, antivirus (e.g. Windows Defender's real-time scanner) and the Windows Search indexer\n * routinely open a freshly-written file for a brief scan without `FILE_SHARE_DELETE`, which makes a\n * rename over/of that file fail transiently with EPERM/EACCES/EBUSY for a few milliseconds. This is\n * a well-documented platform semantic, not a bug in our write path — it's exactly why the\n * `graceful-fs` package ships its own win32 rename-retry wrapper. POSIX platforms never exhibit this\n * transient (a POSIX rename either succeeds or fails for a real, non-transient reason), so retry is\n * gated strictly to win32 to keep POSIX behavior byte-identical to a bare rename.\n */\nfunction isTransientRenameErrorOnWin32(err: unknown): boolean {\n\tif (process.platform !== \"win32\") return false;\n\tif (typeof err !== \"object\" || err === null) return false;\n\tconst code = (err as { code?: string }).code;\n\treturn code === \"EPERM\" || code === \"EACCES\" || code === \"EBUSY\";\n}\n\n/** Sync counterpart of the rename-retry policy; see {@link isTransientRenameErrorOnWin32}. */\nfunction renameSyncWithRetry(tmpPath: string, filePath: string, fs: FaultableFs): void {\n\tfor (let attempt = 0; attempt <= RENAME_RETRY_ATTEMPTS; attempt++) {\n\t\ttry {\n\t\t\tfs.renameSync(tmpPath, filePath);\n\t\t\treturn;\n\t\t} catch (err) {\n\t\t\tif (!isTransientRenameErrorOnWin32(err) || attempt === RENAME_RETRY_ATTEMPTS) throw err;\n\t\t\tconst backoffMs = Math.min(RENAME_RETRY_MIN_TIMEOUT_MS * 2 ** attempt, RENAME_RETRY_MAX_TIMEOUT_MS);\n\t\t\tblockingSleepMs(backoffMs);\n\t\t}\n\t}\n}\n\n/** Async counterpart of the rename-retry policy; see {@link isTransientRenameErrorOnWin32}. */\nasync function renameWithRetry(tmpPath: string, filePath: string): Promise<void> {\n\tfor (let attempt = 0; attempt <= RENAME_RETRY_ATTEMPTS; attempt++) {\n\t\ttry {\n\t\t\tawait fsPromises.rename(tmpPath, filePath);\n\t\t\treturn;\n\t\t} catch (err) {\n\t\t\tif (!isTransientRenameErrorOnWin32(err) || attempt === RENAME_RETRY_ATTEMPTS) throw err;\n\t\t\tconst backoffMs = Math.min(RENAME_RETRY_MIN_TIMEOUT_MS * 2 ** attempt, RENAME_RETRY_MAX_TIMEOUT_MS);\n\t\t\tawait new Promise((resolve) => setTimeout(resolve, backoffMs));\n\t\t}\n\t}\n}\n\nexport interface AtomicFileLockOptions {\n\t/**\n\t * Bounded retry attempts while waiting for a lock already held elsewhere. Both variants use a\n\t * SHORT, capped backoff (see {@link RETRY_MIN_TIMEOUT_MS}/{@link RETRY_MAX_TIMEOUT_MS}) — these\n\t * stores' critical sections are sub-millisecond reads+writes of small JSON/text files, so\n\t * contention should clear in milliseconds, not the multi-second-to-31-second worst case\n\t * proper-lockfile's OWN default backoff produces for a bare numeric `retries` (its default\n\t * `minTimeout` is 1000ms with factor 2 — see node_modules/retry/lib/retry.js). Passing a bare\n\t * number straight through would turn brief contention into a multi-second stall on a hot path\n\t * (e.g. a per-token-stream perf sample), so both variants instead build an explicit short-backoff\n\t * `retry` options object.\n\t * - Async (`withFileLock`): forwarded as `{retries, minTimeout, maxTimeout}` to proper-lockfile.\n\t * - Sync (`withFileLockSync`): proper-lockfile's sync API REJECTS `retries > 0` outright (it\n\t * requires the whole acquire flow to be synchronous — see proper-lockfile/lib/adapter.js\n\t * `toSyncOptions`, which throws `ESYNC`). So the sync path implements its own bounded retry\n\t * around single `lockfile.lockSync` attempts, blocking briefly between them (Atomics.wait) —\n\t * callers are already fully synchronous fs code, so a short blocking wait on contention matches\n\t * the existing execution model rather than introducing a new one.\n\t */\n\tretries?: number;\n\t/** Initial delay between sync/async acquisition attempts; defaults to 25ms. */\n\tminRetryDelayMs?: number;\n\t/** Maximum delay between sync/async acquisition attempts; defaults to 500ms. */\n\tmaxRetryDelayMs?: number;\n\t/** Retry-delay multiplier; defaults to 2. Set max=min for a fixed delay. */\n\tretryFactor?: number;\n\t/** Resolve symlinks before locking (proper-lockfile `realpath`); false matches file-store.ts. */\n\trealpath?: boolean;\n\t/** Lock staleness window in ms (proper-lockfile `stale`); omitted = proper-lockfile's own default. */\n\tstale?: number;\n\t/** Explicit proper-lockfile directory path; defaults to `${filePath}.lock`. */\n\tlockfilePath?: string;\n}\n\nexport interface AtomicFileWriteOptions {\n\t/** POSIX permission bits applied to the temporary file and inherited by the renamed destination. */\n\tmode?: number;\n\t/**\n\t * Injection seam for the mutating fs primitives this write issues (`mkdirSync`, `writeFileSync`,\n\t * `renameSync`). Defaults to real `node:fs` — omitting this option is a zero-behavior-change no-op.\n\t * Only the destructive-testing harness passes a fault-injecting implementation.\n\t */\n\tfs?: FaultableFs;\n}\n\n/** Narrow an unknown Node filesystem failure without unsafe casts at every storage owner. */\nexport function isMissingFileError(error: unknown): boolean {\n\treturn typeof error === \"object\" && error !== null && \"code\" in error && error.code === \"ENOENT\";\n}\n\nconst DEFAULT_RETRIES = 10;\nconst DEFAULT_REALPATH = false;\n/**\n * Capped doubling backoff shared by both variants — see {@link AtomicFileLockOptions.retries}.\n * With the current defaults (10 retries, 25ms floor, 500ms cap) the total worst-case contention\n * budget is ~3.3s (25+50+100+200+400+500+500+500+500+500), comfortably over the >= 2s floor this\n * needs to survive a loaded CI runner (e.g. the Windows CI mkdir/rmdir lock-directory churn under\n * two-real-OS-thread contention).\n */\nconst RETRY_MIN_TIMEOUT_MS = 25;\nconst RETRY_MAX_TIMEOUT_MS = 500;\n\n/**\n * Existing auth/settings/trust paths historically used 10 total attempts separated by 20ms. Keep\n * that low-latency user-facing policy explicit while routing the mechanism through this module.\n */\nexport const LOW_LATENCY_FILE_LOCK_OPTIONS: Readonly<AtomicFileLockOptions> = Object.freeze({\n\tretries: 9,\n\tminRetryDelayMs: 20,\n\tmaxRetryDelayMs: 20,\n});\n\nfunction lockDirectory(filePath: string, lockfilePath?: string): string {\n\treturn dirname(lockfilePath ?? filePath);\n}\n\nfunction ensureLockDirSync(filePath: string, lockfilePath?: string): void {\n\tmkdirSync(lockDirectory(filePath, lockfilePath), { recursive: true });\n}\n\nasync function ensureLockDir(filePath: string, lockfilePath?: string): Promise<void> {\n\tawait fsPromises.mkdir(lockDirectory(filePath, lockfilePath), { recursive: true });\n}\n\nfunction isLockedError(err: unknown): boolean {\n\tif (typeof err !== \"object\" || err === null) return false;\n\tconst code = (err as { code?: string }).code;\n\tif (code === \"ELOCKED\") return true;\n\t// On win32, mkdir-ing the lock directory can transiently surface EPERM (rather than the\n\t// expected EEXIST) when a previous incarnation of that directory is concurrently being\n\t// rmdir'd by the racing releaser — the mkdir lands mid-teardown and the OS reports \"operation\n\t// not permitted\" instead of \"already exists\". This is contention, not a real permissions\n\t// failure, so treat it identically to ELOCKED: retry with the existing backoff rather than\n\t// letting it escape as fatal. POSIX platforms don't exhibit this transient and keep surfacing\n\t// real EPERM (e.g. an actually unwritable directory) as fatal.\n\tif (code === \"EPERM\" && process.platform === \"win32\") return true;\n\treturn false;\n}\n\n/** Block the calling thread for `ms` without spinning the CPU (Atomics.wait on a private buffer). */\nfunction blockingSleepMs(ms: number): void {\n\tAtomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);\n}\n\ntype PendingPathOperations = Map<string, Promise<void>>;\n\nconst pendingAsyncFileLockTails: PendingPathOperations = new Map();\nconst pendingAtomicWriteTails: PendingPathOperations = new Map();\n\nfunction pathOperationKey(filePath: string): string {\n\tconst absolutePath = resolve(filePath);\n\treturn process.platform === \"win32\" ? absolutePath.toLowerCase() : absolutePath;\n}\n\n/**\n * Queue only operations that target the same platform path. This prevents same-process callers\n * from entering synchronized OS retry storms without imposing a process-wide or cross-tenant\n * bottleneck. The tail always resolves and self-removes, including after a failed operation.\n */\nasync function withSerializedPathOperation<T>(\n\ttails: PendingPathOperations,\n\tfilePath: string,\n\tfn: () => Promise<T>,\n): Promise<T> {\n\tconst key = pathOperationKey(filePath);\n\tconst preceding = tails.get(key) ?? Promise.resolve();\n\tlet releaseTail: (() => void) | undefined;\n\tconst tail = new Promise<void>((resolveTail) => {\n\t\treleaseTail = resolveTail;\n\t});\n\ttails.set(key, tail);\n\n\tawait preceding;\n\ttry {\n\t\treturn await fn();\n\t} finally {\n\t\treleaseTail?.();\n\t\tif (tails.get(key) === tail) tails.delete(key);\n\t}\n}\n\n/**\n * Acquire a proper-lockfile sync lock with bounded retry on `ELOCKED` (proper-lockfile's sync API\n * itself forbids `retries > 0`; see {@link AtomicFileLockOptions.retries}).\n *\n * Backoff doubles each attempt (mirroring the `retry` module's own `factor: 2` default that the\n * async path gets for free from proper-lockfile), starting at {@link RETRY_MIN_TIMEOUT_MS} and\n * capped at {@link RETRY_MAX_TIMEOUT_MS}.\n *\n * This is budget PARITY between two implementations of the same lock-acquisition contract, not\n * flake-masking: `options.retries` (a count of RETRIES, i.e. attempts after the first) means the\n * same thing on both paths, so both must make `retries + 1` total acquisition attempts with the\n * same per-gap backoff schedule. The async path gets `retries + 1` attempts for free from the\n * `retry` module (it retries `options.retries` times after its initial attempt); the sync path\n * used to loop exactly `attempts` times total (one fewer gap, and one fewer attempt than the\n * async path for the same `retries` value), which under-budgeted it relative to its async sibling\n * and surfaced as spurious ELOCKED failures under real contention (e.g. two OS threads hammering\n * the same file, or mkdir/rmdir lock-directory churn on a loaded Windows CI runner).\n */\nfunction retryCount(options: AtomicFileLockOptions): number {\n\tconst configured = options.retries ?? DEFAULT_RETRIES;\n\treturn Number.isFinite(configured) ? Math.max(0, Math.floor(configured)) : DEFAULT_RETRIES;\n}\n\nfunction retryDelayMs(options: AtomicFileLockOptions, attempt: number): number {\n\tconst configuredMin = options.minRetryDelayMs ?? RETRY_MIN_TIMEOUT_MS;\n\tconst minDelay = Number.isFinite(configuredMin) ? Math.max(0, configuredMin) : RETRY_MIN_TIMEOUT_MS;\n\tconst configuredMax = options.maxRetryDelayMs ?? RETRY_MAX_TIMEOUT_MS;\n\tconst maxDelay = Number.isFinite(configuredMax) ? Math.max(minDelay, configuredMax) : RETRY_MAX_TIMEOUT_MS;\n\tconst configuredFactor = options.retryFactor ?? 2;\n\tconst factor = Number.isFinite(configuredFactor) ? Math.max(1, configuredFactor) : 2;\n\treturn Math.min(minDelay * factor ** (attempt - 1), maxDelay);\n}\n\n/**\n * Acquire a synchronous advisory file lock and return its release function. This is the shared\n * mechanism for coordinators that must hold several locks in a deterministic order before entering\n * one critical section; callers retain ownership of release ordering and release-error semantics.\n */\nexport function acquireFileLockSync(filePath: string, options: AtomicFileLockOptions = {}): () => void {\n\tensureLockDirSync(filePath, options.lockfilePath);\n\tconst attempts = retryCount(options) + 1;\n\tconst lockOptions = {\n\t\tlockfilePath: options.lockfilePath,\n\t\trealpath: options.realpath ?? DEFAULT_REALPATH,\n\t\tstale: options.stale,\n\t};\n\tfor (let attempt = 1; attempt <= attempts; attempt++) {\n\t\ttry {\n\t\t\treturn lockfile.lockSync(filePath, lockOptions);\n\t\t} catch (err) {\n\t\t\tif (!isLockedError(err) || attempt === attempts) throw err;\n\t\t\tblockingSleepMs(retryDelayMs(options, attempt));\n\t\t}\n\t}\n\t// Unreachable (the loop always returns or throws), but keeps the function's return type honest.\n\tthrow new Error(`Failed to acquire lock for ${filePath}`);\n}\n\n/**\n * Hold an exclusive advisory lock on `filePath` for the duration of `fn` (sync). Always releases,\n * including when `fn` throws.\n */\nexport function withFileLockSync<T>(filePath: string, fn: () => T, options?: AtomicFileLockOptions): T {\n\tconst release = acquireFileLockSync(filePath, options);\n\ttry {\n\t\treturn fn();\n\t} finally {\n\t\t// A lock-cleanup failure must never mask fn()'s result (or replace fn()'s own thrown error) —\n\t\t// by this point fn() has already durably committed or failed on its own terms.\n\t\ttry {\n\t\t\trelease();\n\t\t} catch {\n\t\t\t// best-effort cleanup; a stale lock self-expires via proper-lockfile's `stale` window\n\t\t}\n\t}\n}\n\n/** Async counterpart of {@link withFileLockSync}. Always releases, including when `fn` throws/rejects. */\nexport async function withFileLock<T>(\n\tfilePath: string,\n\tfn: () => Promise<T> | T,\n\toptions?: AtomicFileLockOptions,\n): Promise<T> {\n\treturn withSerializedPathOperation(pendingAsyncFileLockTails, options?.lockfilePath ?? filePath, async () => {\n\t\tawait ensureLockDir(filePath, options?.lockfilePath);\n\t\tconst release = await lockfile.lock(filePath, {\n\t\t\tlockfilePath: options?.lockfilePath,\n\t\t\trealpath: options?.realpath ?? DEFAULT_REALPATH,\n\t\t\tretries: {\n\t\t\t\tretries: retryCount(options ?? {}),\n\t\t\t\tfactor: options?.retryFactor ?? 2,\n\t\t\t\tminTimeout: options?.minRetryDelayMs ?? RETRY_MIN_TIMEOUT_MS,\n\t\t\t\tmaxTimeout: options?.maxRetryDelayMs ?? RETRY_MAX_TIMEOUT_MS,\n\t\t\t},\n\t\t\tstale: options?.stale,\n\t\t});\n\t\ttry {\n\t\t\treturn await fn();\n\t\t} finally {\n\t\t\t// See {@link withFileLockSync} — cleanup failures must not mask fn()'s outcome.\n\t\t\tawait release().catch(() => {});\n\t\t}\n\t});\n}\n\n/**\n * Write `content` to `filePath` via write-tmp-then-rename (sync); the rename is atomic on the same\n * filesystem, so a concurrent reader never observes a partially-written file. Does NOT itself lock —\n * call from inside {@link withFileLockSync} when the write follows a read that must not race another\n * writer's read+write; call standalone for an unconditional overwrite with no read step.\n */\nfunction temporaryPath(filePath: string): string {\n\treturn `${filePath}.${process.pid}.${randomUUID()}.tmp`;\n}\n\n/**\n * Async standalone overwrites can legitimately target the same file at once. Unique temporary\n * names prevent writers from corrupting each other's staging files, but Windows still serializes\n * replacement renames internally. Letting every writer retry in lockstep creates a thundering herd\n * that can exhaust the bounded Defender/indexer retry budget even though no external process owns\n * the destination. Keep one FIFO tail per destination inside this process so each destination has\n * one writer at a time; writes to different destinations remain fully parallel. The tail is always\n * resolved and removed, including after a failed write, so one failure cannot poison later writes\n * or retain tenant paths indefinitely. File-lock queues are separate from atomic-write queues so a\n * write inside a held advisory lock never waits on itself.\n */\nasync function withSerializedAtomicWrite<T>(filePath: string, fn: () => Promise<T>): Promise<T> {\n\treturn withSerializedPathOperation(pendingAtomicWriteTails, filePath, fn);\n}\n\n/** Remove one invocation's temporary path without masking the original write/rename failure. */\nfunction removeTemporaryPathSync(tmpPath: string): void {\n\ttry {\n\t\tunlinkSync(tmpPath);\n\t} catch {\n\t\t// The rename may have consumed the path, or another failure may have prevented its creation.\n\t}\n}\n\nasync function removeTemporaryPath(tmpPath: string): Promise<void> {\n\ttry {\n\t\tawait fsPromises.unlink(tmpPath);\n\t} catch {\n\t\t// The rename may have consumed the path, or another failure may have prevented its creation.\n\t}\n}\n\nexport function writeFileAtomicSync(filePath: string, content: string, options?: AtomicFileWriteOptions): void {\n\tconst fs = options?.fs ?? nodeFs;\n\tfs.mkdirSync(dirname(filePath), { recursive: true });\n\tconst tmpPath = temporaryPath(filePath);\n\tlet renamed = false;\n\ttry {\n\t\t// `wx` makes a nonce collision harmless: never truncate another writer's temporary file.\n\t\tfs.writeFileSync(tmpPath, content, { encoding: \"utf-8\", flag: \"wx\", mode: options?.mode });\n\t\trenameSyncWithRetry(tmpPath, filePath, fs);\n\t\trenamed = true;\n\t} finally {\n\t\tif (!renamed) removeTemporaryPathSync(tmpPath);\n\t}\n}\n\n/** Async counterpart of {@link writeFileAtomicSync}. */\nexport async function writeFileAtomic(\n\tfilePath: string,\n\tcontent: string,\n\toptions?: AtomicFileWriteOptions,\n): Promise<void> {\n\tawait withSerializedAtomicWrite(filePath, async () => {\n\t\tawait fsPromises.mkdir(dirname(filePath), { recursive: true });\n\t\tconst tmpPath = temporaryPath(filePath);\n\t\tlet renamed = false;\n\t\ttry {\n\t\t\t// `wx` makes a nonce collision harmless: never truncate another writer's temporary file.\n\t\t\tawait fsPromises.writeFile(tmpPath, content, { encoding: \"utf-8\", flag: \"wx\", mode: options?.mode });\n\t\t\tawait renameWithRetry(tmpPath, filePath);\n\t\t\trenamed = true;\n\t\t} finally {\n\t\t\tif (!renamed) await removeTemporaryPath(tmpPath);\n\t\t}\n\t});\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"auto-learn-controller.d.ts","sourceRoot":"","sources":["../../../src/modes/interactive/auto-learn-controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAG9D,OAAO,KAAK,EAAE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AO,CAAC,6BAA6B;IAgBrC,OAAO,CAAC,4BAA4B;IAqBpC,OAAO,CAAC,+BAA+B;IA8BvC,eAAe,CACd,MAAM,EAAE,MAAM,EACd,KAAK,UAAQ,EACb,OAAO,GAAE;QACR,YAAY,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;QACrC,UAAU,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;QACnC,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,wBAAwB,CAAC,EAAE,OAAO,CAAC;KAC9B,GACJ,MAAM,CA6HR;IAED,OAAO,CAAC,sBAAsB;IAiF9B;;;OAGG;IACH,yBAAyB,IAAI,OAAO,CAInC;IAED;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,2BAA2B;IAenC,wBAAwB,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,IAAI,CAMvD;IAED,mBAAmB,IAAI,OAAO,CAa7B;IAED,wBAAwB,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,OAAO,CAY1D;IAED,qBAAqB,IAAI,IAAI,CAiB5B;IAED,qBAAqB,IAAI,MAAM,CA8C9B;CACD"}
@@ -18,6 +18,7 @@ import { readAutoLearnSessionIdFromFile, reportCompletedAutoLearnUsageHelper } f
18
18
  import { analyzeReflectionTurn } from "../../core/learning/reflection-turn-analysis.js";
19
19
  import { isProcessAlive } from "../../core/process-liveness.js";
20
20
  import { describeInFlightWorkUnit, getInFlightWorkUnits, getPendingReloadBlockers, } from "../../core/reload-blockers.js";
21
+ import { PI_SESSION_ROLE_ENV } from "../../core/session-role.js";
21
22
  import { checkTaskStepsContract, INITIAL_TASK_CONTRACT_STREAK, } from "../../core/tasks/task-contract-monitor.js";
22
23
  import { getProcessWorkRun } from "../../utils/work-directory.js";
23
24
  import { theme } from "./theme/theme.js";
@@ -812,6 +813,7 @@ export class AutoLearnController {
812
813
  env: {
813
814
  ...process.env,
814
815
  PI_AUTO_LEARN_CHILD: "1",
816
+ [PI_SESSION_ROLE_ENV]: "worker",
815
817
  ...(sourceSessionFile ? { PI_AUTO_LEARN_SOURCE_SESSION_FILE: sourceSessionFile } : {}),
816
818
  },
817
819
  });