@tomflow/chatloop-tools 0.9.64 → 0.9.70

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 (175) hide show
  1. package/node_modules/@tomflow/chatloop-context/README.md +296 -0
  2. package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
  3. package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
  4. package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
  5. package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +290 -0
  6. package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
  7. package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
  8. package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
  9. package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
  10. package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
  11. package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
  12. package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
  13. package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +261 -0
  14. package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
  15. package/node_modules/@tomflow/chatloop-context/index.mjs +238 -0
  16. package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
  17. package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +77 -0
  18. package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +2036 -0
  19. package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +69 -0
  20. package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +338 -0
  21. package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
  22. package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +286 -0
  23. package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +117 -0
  24. package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
  25. package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +134 -0
  26. package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
  27. package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
  28. package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +180 -0
  29. package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +68 -0
  30. package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
  31. package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +367 -0
  32. package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +398 -0
  33. package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +264 -0
  34. package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
  35. package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
  36. package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
  37. package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +118 -0
  38. package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
  39. package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +316 -0
  40. package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
  41. package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
  42. package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +117 -0
  43. package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +142 -0
  44. package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
  45. package/node_modules/@tomflow/chatloop-context/package.json +38 -0
  46. package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
  47. package/node_modules/@tomflow/chatloop-extension/README.md +190 -0
  48. package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +13 -0
  49. package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +168 -0
  50. package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +246 -0
  51. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +1 -0
  52. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +1 -0
  53. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +1 -0
  54. package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +1 -0
  55. package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +1 -0
  56. package/node_modules/@tomflow/chatloop-extension/capabilities/entry_test.mjs +1 -0
  57. package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +1 -0
  58. package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +1 -0
  59. package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +1 -0
  60. package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +1 -0
  61. package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +1 -0
  62. package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +123 -0
  63. package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +183 -0
  64. package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +390 -0
  65. package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
  66. package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +241 -0
  67. package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
  68. package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +399 -0
  69. package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
  70. package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +60 -0
  71. package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +9 -0
  72. package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +20 -0
  73. package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +77 -0
  74. package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +95 -0
  75. package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +329 -0
  76. package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +38 -0
  77. package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
  78. package/node_modules/@tomflow/chatloop-extension/extension/background.js +33 -0
  79. package/node_modules/@tomflow/chatloop-extension/extension/clean.html +5 -0
  80. package/node_modules/@tomflow/chatloop-extension/extension/content.js +40 -0
  81. package/node_modules/@tomflow/chatloop-extension/extension/continue.html +9 -0
  82. package/node_modules/@tomflow/chatloop-extension/extension/dom.html +17 -0
  83. package/node_modules/@tomflow/chatloop-extension/extension/entry_test.html +6 -0
  84. package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +9 -0
  85. package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +2 -0
  86. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +275 -0
  87. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +84 -0
  88. package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +107 -0
  89. package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +36 -0
  90. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +299 -0
  91. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +144 -0
  92. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +172 -0
  93. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
  94. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +211 -0
  95. package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
  96. package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +399 -0
  97. package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +43 -0
  98. package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
  99. package/node_modules/@tomflow/chatloop-extension/extension/popup.js +99 -0
  100. package/node_modules/@tomflow/chatloop-extension/extension/reload.html +17 -0
  101. package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +17 -0
  102. package/node_modules/@tomflow/chatloop-extension/extension/session.html +7 -0
  103. package/node_modules/@tomflow/chatloop-extension/extension/start.html +6 -0
  104. package/node_modules/@tomflow/chatloop-extension/extension/status.html +6 -0
  105. package/node_modules/@tomflow/chatloop-extension/extension/text.html +17 -0
  106. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +136 -0
  107. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +218 -0
  108. package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +127 -0
  109. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +296 -0
  110. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +186 -0
  111. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +126 -0
  112. package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +287 -0
  113. package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +163 -0
  114. package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +345 -0
  115. package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +209 -0
  116. package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +385 -0
  117. package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +75 -0
  118. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-action-v2.js +278 -0
  119. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-recovery-v2.js +52 -0
  120. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-v2.js +93 -0
  121. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-continue-recovery-v2.js +78 -0
  122. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-recovery-v2.js +148 -0
  123. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-v2.js +325 -0
  124. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-hold-recovery-v2.js +109 -0
  125. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-dispatch-v2.js +220 -0
  126. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-v2.js +106 -0
  127. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-owner-resolver.js +110 -0
  128. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-recover-v2.js +209 -0
  129. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-routes-v2.js +357 -0
  130. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-run-store.js +400 -0
  131. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-scheduler.js +78 -0
  132. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-start-v2.js +361 -0
  133. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-terminal-v2.js +235 -0
  134. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +197 -0
  135. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +205 -0
  136. package/node_modules/@tomflow/chatloop-extension/index.mjs +78 -0
  137. package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +19 -0
  138. package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +82 -0
  139. package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +378 -0
  140. package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +89 -0
  141. package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
  142. package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +122 -0
  143. package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +75 -0
  144. package/node_modules/@tomflow/chatloop-extension/lib/owner-kernel-lock.mjs +106 -0
  145. package/node_modules/@tomflow/chatloop-extension/lib/owner-ledger.mjs +316 -0
  146. package/node_modules/@tomflow/chatloop-extension/lib/owner-locked-write.mjs +30 -0
  147. package/node_modules/@tomflow/chatloop-extension/lib/scoped-cancel-v2.mjs +190 -0
  148. package/node_modules/@tomflow/chatloop-extension/lib/scoped-continue-v2.mjs +214 -0
  149. package/node_modules/@tomflow/chatloop-extension/lib/scoped-handoff-v2.mjs +338 -0
  150. package/node_modules/@tomflow/chatloop-extension/lib/scoped-native-control.mjs +398 -0
  151. package/node_modules/@tomflow/chatloop-extension/lib/scoped-recovery-v2.mjs +234 -0
  152. package/node_modules/@tomflow/chatloop-extension/lib/scoped-retire-v2.mjs +70 -0
  153. package/node_modules/@tomflow/chatloop-extension/lib/scoped-terminal-v2.mjs +143 -0
  154. package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +228 -0
  155. package/node_modules/@tomflow/chatloop-extension/package.json +36 -0
  156. package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +138 -0
  157. package/node_modules/@tomflow/chatloop-trace/README.md +118 -0
  158. package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
  159. package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
  160. package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
  161. package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
  162. package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
  163. package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +308 -0
  164. package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +140 -0
  165. package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +236 -0
  166. package/node_modules/@tomflow/chatloop-trace/package.json +28 -0
  167. package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
  168. package/node_modules/ps-list/index.d.ts +76 -0
  169. package/node_modules/ps-list/index.js +366 -0
  170. package/node_modules/ps-list/license +9 -0
  171. package/node_modules/ps-list/package.json +44 -0
  172. package/node_modules/ps-list/readme.md +56 -0
  173. package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
  174. package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
  175. package/package.json +12 -4
@@ -0,0 +1,69 @@
1
+ // Composes capabilities declared in the build manifest, never scans at runtime.
2
+ // Domain methods stay behind the existing Store's authority/CAS/receipt gates.
3
+ // New capability routes use a restricted, project-scoped read-only service port.
4
+ const fail=code=>Object.assign(new Error(code),{code});
5
+ const isObject=x=>x!==null&&typeof x==='object'&&!Array.isArray(x);
6
+ const value=(spec,args)=>{
7
+ if(spec==='$args')return args;
8
+ if(typeof spec==='string')return args[spec];
9
+ if(isObject(spec)&&isObject(spec.$object))
10
+ return Object.fromEntries(Object.entries(spec.$object).map(([key,x])=>
11
+ [key,isObject(x)&&typeof x.$field==='string'
12
+ ?args[x.$field]??x.$default
13
+ :x]));
14
+ throw fail('CAPABILITY_ROUTE_PARAMETER_INVALID');
15
+ };
16
+
17
+ export function createCapabilityRouter(definitions,capabilities){
18
+ if(!Array.isArray(definitions)||!Array.isArray(capabilities))
19
+ throw fail('CAPABILITY_REGISTRY_INVALID');
20
+ const registry=new Map();
21
+ for(const cap of capabilities){
22
+ if(!cap||typeof cap.id!=='string'||!Array.isArray(cap.methods))
23
+ throw fail('CAPABILITY_REGISTRY_INVALID');
24
+ for(const entry of cap.methods){
25
+ if(registry.has(entry.id))throw fail('CAPABILITY_METHOD_CONFLICT');
26
+ registry.set(entry.id,{cap,entry});
27
+ }
28
+ }
29
+ if(registry.size!==definitions.length)throw fail('CAPABILITY_MANIFEST_MISMATCH');
30
+ for(const def of definitions){
31
+ const source=registry.get(def.id);
32
+ if(!source||JSON.stringify(source.entry.route)!==JSON.stringify(def.route)
33
+ ||JSON.stringify(source.entry.inputSchema)!==JSON.stringify(def.inputSchema)
34
+ ||source.entry.description!==def.description
35
+ ||source.entry.mode!==def.mode||source.entry.group!==def.group)
36
+ throw fail('CAPABILITY_MANIFEST_MISMATCH');
37
+ }
38
+ return Object.freeze({
39
+ execute(method,args,services){
40
+ const source=registry.get(method?.id);
41
+ if(!source||source.entry.mode!==method.mode)
42
+ throw fail('CAPABILITY_METHOD_UNKNOWN');
43
+ // The capability registry was verified against the build manifest.
44
+ // Never dispatch a supplied method object's mutable route instead.
45
+ const route=source.entry.route;
46
+ if(route.kind==='core'){
47
+ const fn=services?.db?.[route.method];
48
+ if(typeof fn!=='function')throw fail('CAPABILITY_CORE_PROVIDER_MISSING');
49
+ return Reflect.apply(fn,services.db,
50
+ route.params.map(param=>value(param,args)));
51
+ }
52
+ if(route.kind==='module'){
53
+ if(method.mode!=='read'||typeof source.cap.execute!=='function'
54
+ ||args.projectId!==services?.projectRef)
55
+ throw fail('CAPABILITY_PERMISSION_DENIED');
56
+ const read=Object.freeze({
57
+ projectRef:args.projectId,
58
+ // No raw DB handle, credential, CAS mutation or cross-project lookup.
59
+ material:(materialId,version)=>
60
+ services.db.projectMaterialGet(args.projectId,materialId,version),
61
+ document:(taskId,documentId,version)=>
62
+ services.db.documentGet(args.projectId,taskId,documentId,version)
63
+ });
64
+ return source.cap.execute(method.id,args,read);
65
+ }
66
+ throw fail('CAPABILITY_ADAPTER_ROUTE_REQUIRED');
67
+ }
68
+ });
69
+ }
@@ -0,0 +1,338 @@
1
+ // CLI command implementation: no imports from other internal modules.
2
+ // Only bin/cli.mjs exports a shell command. index.mjs supplies scoped API
3
+ // dependencies; local admin/SQLite safety gates remain in their owners.
4
+ import {randomUUID,createHash} from 'node:crypto';
5
+ import {lstatSync,mkdirSync,mkdtempSync,renameSync,rmSync} from 'node:fs';
6
+ import {join} from 'node:path';
7
+
8
+ const fail=code=>Object.assign(new Error(code),{code});
9
+ // Reject dangling target links as occupied rather than replacing them.
10
+ const occupied=path=>{
11
+ try{lstatSync(path);return true}
12
+ catch(error){if(error.code==='ENOENT')return false;throw error}
13
+ };
14
+ let openContextProtocol,contextRoot,allowedProjects,
15
+ verifySourceUnit,createProjectDiagnosticBranch,createManagedProjectBranch,
16
+ fenceLegacyProjectStore,auditLegacyProjectSources,inspectLegacyRegistryReference,
17
+ activeLegacyWriters,fenceOrphanRunOperator,canonicalWorkspaceRefs,
18
+ confirmWorkspaceOperator,assertWorkspaceUnclaimed,withWorkspaceRegistryLock,
19
+ rebindProjectWorkspaces,readOperatorWorkspaceReceipt,runContextCli,
20
+ requestContext,listMethods,describeMethod,CONTEXT_PACKAGE_VERSION;
21
+ let connected=false;
22
+ export function configureCliCommands(api){
23
+ if(connected)throw Error('CONTEXT_MODULE_ALREADY_COMPOSED');
24
+ const names=['openContextProtocol','contextRoot','allowedProjects',
25
+ 'verifySourceUnit','createProjectDiagnosticBranch','createManagedProjectBranch',
26
+ 'fenceLegacyProjectStore','auditLegacyProjectSources',
27
+ 'inspectLegacyRegistryReference','activeLegacyWriters',
28
+ 'fenceOrphanRunOperator','canonicalWorkspaceRefs',
29
+ 'confirmWorkspaceOperator','assertWorkspaceUnclaimed','withWorkspaceRegistryLock',
30
+ 'rebindProjectWorkspaces','readOperatorWorkspaceReceipt','runContextCli',
31
+ 'requestContext','listMethods','describeMethod','CONTEXT_PACKAGE_VERSION'];
32
+ for(const name of names)
33
+ if(api?.[name]===undefined)throw Error('CONTEXT_MODULE_PORT_MISSING:'+name);
34
+ ({openContextProtocol,contextRoot,allowedProjects,
35
+ verifySourceUnit,createProjectDiagnosticBranch,createManagedProjectBranch,
36
+ fenceLegacyProjectStore,auditLegacyProjectSources,inspectLegacyRegistryReference,
37
+ activeLegacyWriters,fenceOrphanRunOperator,canonicalWorkspaceRefs,
38
+ confirmWorkspaceOperator,assertWorkspaceUnclaimed,withWorkspaceRegistryLock,
39
+ rebindProjectWorkspaces,readOperatorWorkspaceReceipt,runContextCli,
40
+ requestContext,listMethods,describeMethod,CONTEXT_PACKAGE_VERSION}=api);
41
+ connected=true;
42
+ }
43
+
44
+ async function registerProject(projectId,title,workspaces=[],reviewed=false){
45
+ if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
46
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
47
+ if(!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(projectId))throw fail('INVALID_SCOPE');
48
+ if(typeof title!=='string'||!title.trim()||title.length>200)throw fail('INVALID_TITLE');
49
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
50
+ const refs=canonicalWorkspaceRefs(workspaces,{allowEmpty:!reviewed});
51
+ const root=contextRoot(),final=join(root,projectId);
52
+ if(occupied(final))throw fail('PROJECT_EXISTS');
53
+ // The user-reviewed entry never creates a Store (or even a directory)
54
+ // before the local operator confirms the exact canonical roots.
55
+ if(reviewed)await confirmWorkspaceOperator('REGISTER',projectId,0,refs);
56
+ mkdirSync(root,{recursive:true,mode:0o700});
57
+ return withWorkspaceRegistryLock(root,()=>{
58
+ let tmp;
59
+ try{
60
+ if(occupied(final))throw fail('PROJECT_EXISTS');
61
+ if(reviewed){
62
+ const current=canonicalWorkspaceRefs(workspaces);
63
+ if(JSON.stringify(current)!==JSON.stringify(refs))throw fail('WORKSPACE_CHANGED');
64
+ assertWorkspaceUnclaimed(root,projectId,refs);
65
+ }
66
+ tmp=mkdtempSync(join(root,'.register-'));
67
+ const db=openContextProtocol(join(tmp,'context.sqlite'),{
68
+ allowCreate:true,authorizeData:()=>true,principalId:'local-operator'});
69
+ let result;
70
+ try{
71
+ result=db.projectCreate({projectId,title,sharedContext:{workspaceRefs:refs},
72
+ expectedStoreEpoch:db.epoch,writeRequestId:randomUUID()});
73
+ }finally{db.close()}
74
+ renameSync(tmp,final);tmp=null;
75
+ return {...result,workspaceRefs:refs,projectDirectory:final};
76
+ }finally{
77
+ // Incomplete staging is diagnostic only, never silently promoted.
78
+ if(tmp)rmSync(tmp,{recursive:true,force:true});
79
+ }
80
+ });
81
+ }
82
+ export async function runLocalCliCommand(args,readJson){
83
+ const [command,sub,...rest]=args;
84
+ let value;
85
+ if(command==='methods'&&sub==='list'&&rest.length<=1)
86
+ value=await requestContext({operation:'sdk.methods.list',
87
+ ...(rest[0]&&rest[0]!=='--json'?{group:rest[0]}:{})});
88
+ else if(command==='methods'&&sub==='groups'&&rest.length<=1)
89
+ value=await requestContext({operation:'sdk.methods.list',view:'groups',
90
+ ...(rest[0]?{group:rest[0]}:{})});
91
+ else if(command==='methods'&&sub==='describe'&&rest.length===1)
92
+ value=await requestContext({operation:'sdk.methods.describe',methodId:rest[0]});
93
+ else if(command==='admin'&&sub){
94
+ value=await runContextCli([sub,...rest]);
95
+ }
96
+ else if(command==='request'&&sub==='--json'&&rest.length===0){
97
+ const payload=await readJson();
98
+ if(!payload||typeof payload!=='object'||Array.isArray(payload))throw fail('INVALID_ARGUMENT');
99
+ value=await requestContext(payload);
100
+ }else if(command==='task'&&sub==='fence-orphan'&&rest.length===10){
101
+ const [projectId,taskId,expectedRunRef,revision,bindingRevision,
102
+ expectedStoreEpoch,evidenceDocumentId,evidenceVersion,evidenceHash,
103
+ writeRequestId]=rest;
104
+ value=await fenceOrphanRunOperator({projectId,taskId,expectedRunRef,
105
+ expectedTaskRevision:Number(revision),
106
+ expectedBindingRevision:Number(bindingRevision),
107
+ expectedStoreEpoch,evidenceDocumentId,
108
+ evidenceVersion:Number(evidenceVersion),evidenceHash,writeRequestId});
109
+ }else if(command==='task'&&sub==='correct-review-scope'&&rest.length===8){
110
+ // Independent, explicit local operator action; NOT exposed via Context MCP.
111
+ const [projectId,taskId,revision,approvalRevision,storeEpoch,
112
+ expectedPermission,operatorEvidenceRef,writeRequestId]=rest;
113
+ if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
114
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
115
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
116
+ const expectedTaskRevision=Number(revision),
117
+ expectedApprovalRevision=Number(approvalRevision);
118
+ if(!Number.isSafeInteger(expectedTaskRevision)||expectedTaskRevision<1
119
+ ||!Number.isSafeInteger(expectedApprovalRevision)||expectedApprovalRevision<1)
120
+ throw fail('SCOPE_CORRECTION_REVISION_INVALID');
121
+ if(!['REQUIREMENTS_REVIEW_ONLY','PLAN_REVIEW_ONLY'].includes(expectedPermission)
122
+ ||typeof operatorEvidenceRef!=='string'||operatorEvidenceRef.length<8
123
+ ||operatorEvidenceRef.length>256
124
+ ||typeof writeRequestId!=='string'||writeRequestId.length<8
125
+ ||writeRequestId.length>128)
126
+ throw fail('SCOPE_CORRECTION_ARGUMENT_INVALID');
127
+ const file=join(contextRoot(),projectId,'context.sqlite');
128
+ const read=openContextProtocol(file,{readOnly:true,principalId:'local-operator',
129
+ authorizeData:(_mode,p)=>p===projectId});
130
+ try{
131
+ const current=read.get(projectId,taskId);
132
+ if(read.epoch!==storeEpoch)throw fail('STALE_EPOCH');
133
+ if(current.taskRevision!==expectedTaskRevision)
134
+ throw fail('BUSINESS_VERSION_CONFLICT');
135
+ if(current.approvalRevision!==expectedApprovalRevision)
136
+ throw fail('APPROVAL_CONFLICT');
137
+ if(current.currentContext.executionPermission!==expectedPermission)
138
+ throw fail('SCOPE_CORRECTION_NOT_ELIGIBLE');
139
+ if(read.projectBusinessAuthority(projectId).writable!==true)
140
+ throw fail('PROJECT_CUTOVER_NOT_VERIFIED');
141
+ }finally{read.close()}
142
+ await confirmWorkspaceOperator('CORRECT_REVIEW_SCOPE',projectId,
143
+ expectedTaskRevision,[taskId,expectedPermission,
144
+ String(expectedApprovalRevision),storeEpoch,operatorEvidenceRef]);
145
+ const db=openContextProtocol(file,{principalId:'local-operator',
146
+ authorizeData:(_mode,p)=>p===projectId,
147
+ authorizeAdmin:(op,p)=>op==='task.reviewScope.correct'&&p===projectId});
148
+ try{
149
+ value=db.taskCorrectReviewScope({projectId,taskId,
150
+ expectedTaskRevision,expectedApprovalRevision,
151
+ expectedPermission,operatorEvidenceRef,
152
+ reason:'STAGE_ONLY_INTENT_MISCLASSIFIED',
153
+ expectedStoreEpoch:storeEpoch,writeRequestId});
154
+ const after=db.get(projectId,taskId);
155
+ if(after.taskRevision!==value.taskRevision
156
+ ||after.bindingRevision!==0||after.runRef!==null
157
+ ||Object.hasOwn(after.currentContext,'executionPermission')
158
+ ||after.currentContext.scopeCorrection?.operatorEvidenceRef!==operatorEvidenceRef)
159
+ throw fail('SCOPE_CORRECTION_READBACK_FAILED');
160
+ value={...value,projectId,taskId,storeEpoch,writeRequestId};
161
+ }finally{db.close()}
162
+ }else if(command==='task'&&['orphan-receipt','scope-correction-receipt'].includes(sub)&&rest.length===3){
163
+ const [projectId,storeEpoch,writeRequestId]=rest;
164
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
165
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
166
+ const file=join(contextRoot(),projectId,'context.sqlite');
167
+ const db=openContextProtocol(file,{readOnly:true,principalId:'local-operator',
168
+ authorizeData:(_mode,p)=>p===projectId});
169
+ try{value=db.requestStatus({projectId,storeEpoch,writeRequestId})}
170
+ finally{db.close()}
171
+ }else if(command==='project'&&sub==='init'&&(rest.length===2||rest.length===3))
172
+ value=await registerProject(rest[0],rest[1],rest.slice(2));
173
+ else if(command==='project'&&sub==='register'&&rest.length>=3&&rest.length<=18)
174
+ value=await registerProject(rest[0],rest[1],rest.slice(2),true);
175
+ else if(command==='project'&&sub==='bind'&&rest.length>=4&&rest.length<=20){
176
+ const [projectId,revision,epoch,requestId,...paths]=rest;
177
+ if(paths.length===1&&paths[0]==='--clear')paths.length=0;
178
+ value=await rebindProjectWorkspaces({projectId,
179
+ expectedProjectRevision:Number(revision),expectedStoreEpoch:epoch,
180
+ writeRequestId:requestId,workspaces:paths});
181
+ }else if(command==='project'&&sub==='receipt'&&rest.length===3)
182
+ value=await readOperatorWorkspaceReceipt({projectId:rest[0],
183
+ storeEpoch:rest[1],writeRequestId:rest[2]});
184
+ else if(command==='project'&&sub==='reconcile-reference'&&rest.length===10){
185
+ const [projectId,sourceDb,expectedStoreEpoch,sourceUnitId,revision,
186
+ taskId,originalArchivePath,expectedCurrentHash,reviewRef,writeRequestId]=rest;
187
+ if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
188
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
189
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
190
+ const expectedMigrationRevision=Number(revision);
191
+ if(!Number.isSafeInteger(expectedMigrationRevision)||expectedMigrationRevision<1)
192
+ throw fail('MIGRATION_VERSION_CONFLICT');
193
+ // Fail closed before any writable handle exists. Review is NOT user R/P.
194
+ const evidence=inspectLegacyRegistryReference({sourceDb,projectId,
195
+ expectedStoreEpoch,sourceUnitId,taskId,archiveFile:originalArchivePath});
196
+ if(evidence.changeKind!=='INSERTIONS_ONLY'||
197
+ evidence.currentTaskHash!==expectedCurrentHash)
198
+ throw fail('REGISTRY_REFERENCE_VERSION_CONFLICT');
199
+ await confirmWorkspaceOperator('RECONCILE_REFERENCE',projectId,
200
+ expectedMigrationRevision,[sourceDb,sourceUnitId,taskId,
201
+ evidence.archivedTaskHash,expectedCurrentHash,reviewRef]);
202
+ const db=openContextProtocol(sourceDb,{principalId:'local-operator',
203
+ authorizeData:(_mode,p)=>p===projectId,
204
+ authorizeAdmin:(op,p)=>p===projectId&&[
205
+ 'migration.reference.reconcile',
206
+ 'migration.reference.reconcile.reviewed'].includes(op)});
207
+ try{
208
+ value=db.migrationReconcileReference({projectId,sourceUnitId,
209
+ expectedMigrationRevision,expectedStoreEpoch,writeRequestId,
210
+ taskId,originalArchivePath,expectedCurrentHash,
211
+ sourceHash:evidence.sourceHash,reviewRef});
212
+ }finally{db.close()}
213
+ }else if(command==='project'&&sub==='migration-receipt'&&rest.length===4){
214
+ const [projectId,sourceDb,storeEpoch,writeRequestId]=rest;
215
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
216
+ const db=openContextProtocol(sourceDb,{readOnly:true,
217
+ principalId:'local-operator',
218
+ authorizeData:(_mode,p)=>p===projectId});
219
+ try{value=db.requestStatus({projectId,storeEpoch,writeRequestId})}
220
+ finally{db.close()}
221
+ }else if(command==='project'&&sub==='classify-evidence'&&rest.length===8){
222
+ const [projectId,sourceDb,expectedStoreEpoch,sourceUnitId,revision,
223
+ sourceHash,reviewRef,writeRequestId]=rest;
224
+ if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
225
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
226
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
227
+ const expectedMigrationRevision=Number(revision);
228
+ if(!Number.isSafeInteger(expectedMigrationRevision)||expectedMigrationRevision<1)
229
+ throw fail('MIGRATION_VERSION_CONFLICT');
230
+ if(typeof reviewRef!=='string'||reviewRef.length<8||reviewRef.length>256)
231
+ throw fail('REVIEW_REFERENCE_REQUIRED');
232
+ // The historical body's original bytes must already be a matching
233
+ // LEGACY_UNVERIFIED Project Material, never a synthesized current Task.
234
+ const reader=openContextProtocol(sourceDb,{readOnly:true,
235
+ principalId:'local-operator',authorizeData:(_mode,p)=>p===projectId,
236
+ authorizeAdmin:(op,p)=>op==='migration.list'&&p===projectId});
237
+ let unit;
238
+ try{
239
+ if(reader.mode!=='ACTIVE')throw fail('STORE_DIAGNOSTIC_ONLY');
240
+ if(reader.epoch!==expectedStoreEpoch)throw fail('STALE_EPOCH');
241
+ unit=reader.migrationList(projectId).find(x=>x.sourceUnitId===sourceUnitId);
242
+ if(!unit||unit.status!=='SHADOW_RO'||
243
+ unit.manifest.sourceKind!=='MIXED_CONTEXT')
244
+ throw fail('MIXED_SOURCE_NOT_SHADOW');
245
+ if(unit.migrationRevision!==expectedMigrationRevision)
246
+ throw fail('MIGRATION_VERSION_CONFLICT');
247
+ if(unit.sourceHash!==sourceHash)throw fail('SOURCE_CHANGED');
248
+ verifySourceUnit(unit.manifest);
249
+ const material=reader.projectMaterialList(projectId).filter(m=>
250
+ m.sourcePath===unit.sourcePath&&m.contentHash===sourceHash&&
251
+ m.provenance==='LEGACY_UNVERIFIED');
252
+ if(!material.some(m=>createHash('sha256').update(
253
+ reader.projectMaterialGet(projectId,m.materialId,m.version).body
254
+ ).digest('hex')===sourceHash))throw fail('MIGRATION_BODY_NOT_IMPORTED');
255
+ }finally{reader.close()}
256
+ // Human-reviewed disposition: preserve FULL mixed history as
257
+ // EvidenceOnly/UNKNOWN. Do not assert any current Task or R/P fact.
258
+ await confirmWorkspaceOperator('CLASSIFY_EVIDENCE_ONLY',projectId,
259
+ expectedMigrationRevision,[sourceDb,sourceUnitId,sourceHash,reviewRef]);
260
+ const db=openContextProtocol(sourceDb,{principalId:'local-operator',
261
+ authorizeData:(_mode,p)=>p===projectId,
262
+ authorizeAdmin:(op,p)=>p===projectId&&
263
+ ['migration.classify','migration.classify.reviewed'].includes(op)});
264
+ try{
265
+ value=db.migrationClassify({projectId,sourceUnitId,sourceHash,
266
+ expectedStoreEpoch,expectedMigrationRevision,writeRequestId,
267
+ classification:{projectId,sourceUnitId,sourceHash,
268
+ currentFactsComplete:true,reviewRef,taskContexts:[],
269
+ projectSharedContext:{},
270
+ unknownHistoryRefs:[{locator:unit.sourcePath,sourceHash}]}});
271
+ }finally{db.close()}
272
+ }else if(command==='project'&&sub==='inspect-reference'&&rest.length===6){
273
+ const [projectId,sourceDb,expectedStoreEpoch,sourceUnitId,taskId,archiveFile]=rest;
274
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
275
+ value=inspectLegacyRegistryReference({projectId,sourceDb,
276
+ expectedStoreEpoch,sourceUnitId,taskId,archiveFile});
277
+ }else if(command==='project'&&sub==='audit-sources'&&rest.length===3){
278
+ const [projectId,sourceDb,expectedStoreEpoch]=rest;
279
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
280
+ value=auditLegacyProjectSources({projectId,sourceDb,expectedStoreEpoch});
281
+ // Observe known, supported MCP writers without claiming an independent
282
+ // old-writer fence. Only PIDs are returned, never process argv or secrets.
283
+ let writerObservation;
284
+ try{
285
+ const pids=await activeLegacyWriters();
286
+ writerObservation={status:pids.length?'MATCHED':'NO_SUPPORTED_PROCESS_MATCH',pids};
287
+ }catch{writerObservation={status:'UNVERIFIED',pids:[]}}
288
+ value={...value,supportedWriterObservation:writerObservation};
289
+ }else if(command==='project'&&sub==='seal-source'&&rest.length===6){
290
+ const [projectId,sourceDb,epoch,revision,backupPath,sealManifestPath]=rest;
291
+ if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
292
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
293
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
294
+ const expectedProjectRevision=Number(revision);
295
+ if(!Number.isSafeInteger(expectedProjectRevision)||expectedProjectRevision<1)
296
+ throw fail('PROJECT_VERSION_CONFLICT');
297
+ await confirmWorkspaceOperator('SEAL_SOURCE',projectId,
298
+ expectedProjectRevision,[sourceDb,epoch,backupPath,sealManifestPath]);
299
+ value=await fenceLegacyProjectStore({sourceDb,projectId,
300
+ expectedStoreEpoch:epoch,expectedProjectRevision,backupPath,sealManifestPath});
301
+ }else if(command==='project'&&sub==='publish-managed'&&rest.length===6){
302
+ const [projectId,sourceDb,epoch,revision,sealManifest,expectedSealHash]=rest;
303
+ if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
304
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
305
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
306
+ const expectedProjectRevision=Number(revision);
307
+ if(!Number.isSafeInteger(expectedProjectRevision)||expectedProjectRevision<1)
308
+ throw fail('PROJECT_VERSION_CONFLICT');
309
+ await confirmWorkspaceOperator('PUBLISH_MANAGED',projectId,
310
+ expectedProjectRevision,[sourceDb,epoch,sealManifest,expectedSealHash]);
311
+ const root=contextRoot();
312
+ mkdirSync(root,{recursive:true,mode:0o700});
313
+ value=await withWorkspaceRegistryLock(root,()=>createManagedProjectBranch({
314
+ sourceDb,targetRoot:root,projectId,expectedStoreEpoch:epoch,
315
+ expectedProjectRevision,sealManifest,expectedSealHash
316
+ }));
317
+ }else if(command==='project'&&sub==='stage-diagnostic'&&rest.length===4){
318
+ const [projectId,sourceDb,expectedStoreEpoch,revision]=rest;
319
+ if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
320
+ if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
321
+ if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
322
+ const expectedProjectRevision=Number(revision);
323
+ if(!Number.isSafeInteger(expectedProjectRevision)||expectedProjectRevision<1)
324
+ throw fail('PROJECT_VERSION_CONFLICT');
325
+ // This is an offline diagnostic extract; NEVER a source cutover.
326
+ // The operator must review the exact source path and source CAS identity.
327
+ await confirmWorkspaceOperator('STAGE_DIAGNOSTIC',projectId,
328
+ expectedProjectRevision,[sourceDb,expectedStoreEpoch]);
329
+ const root=contextRoot();
330
+ mkdirSync(root,{recursive:true,mode:0o700});
331
+ value=await withWorkspaceRegistryLock(root,()=>createProjectDiagnosticBranch({
332
+ sourceDb,targetRoot:root,projectId,expectedStoreEpoch,expectedProjectRevision
333
+ }));
334
+ }else if(command==='doctor'&&sub==='--json'&&rest.length===0)
335
+ value={status:'READY',packageVersion:CONTEXT_PACKAGE_VERSION,projectRoot:contextRoot()};
336
+ else throw fail('UNKNOWN_COMMAND');
337
+ return value;
338
+ }