@tomflow/chatloop-tools 0.4.29 → 0.4.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -7
- package/node_modules/@tomflow/chatloop-context/README.md +0 -204
- package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +0 -236
- package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +0 -301
- package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +0 -272
- package/node_modules/@tomflow/chatloop-context/lib/context-protocol.mjs +0 -1595
- package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +0 -264
- package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +0 -219
- package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +0 -289
- package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +0 -159
- package/node_modules/@tomflow/chatloop-context/package.json +0 -21
- package/node_modules/@tomflow/chatloop-trace/README.md +0 -13
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +0 -259
- package/node_modules/@tomflow/chatloop-trace/package.json +0 -11
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.4.
|
|
6
|
+
"version": "0.4.30",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"description": "Chat Loop local tools: MCP gateway, isolated bundled tunnel-client and unified CLI",
|
|
9
9
|
"bin": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@wonderwhy-er/desktop-commander": "0.2.44",
|
|
25
|
-
"@tomflow/chatloop-context": "0.1.
|
|
25
|
+
"@tomflow/chatloop-context": "0.1.1",
|
|
26
26
|
"@tomflow/chatloop-trace": "0.1.0"
|
|
27
27
|
},
|
|
28
28
|
"engines": {
|
|
@@ -31,9 +31,5 @@
|
|
|
31
31
|
"optionalDependencies": {
|
|
32
32
|
"@tomflow/chatloop-tunnel-darwin-x64": "0.0.19",
|
|
33
33
|
"@tomflow/chatloop-tunnel-darwin-arm64": "0.0.19"
|
|
34
|
-
}
|
|
35
|
-
"bundleDependencies": [
|
|
36
|
-
"@tomflow/chatloop-context",
|
|
37
|
-
"@tomflow/chatloop-trace"
|
|
38
|
-
]
|
|
34
|
+
}
|
|
39
35
|
}
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
# @tomflow/chatloop-context — Context SDK and CLI
|
|
2
|
-
|
|
3
|
-
Independent Context SDK. Method Catalog supports Node >=20; CLI/Core require Node >=24. Chat Loop Tools and other explicitly
|
|
4
|
-
authorized local adapters use the same versioned Method Catalog and SQLite Core.
|
|
5
|
-
This package is not a Chrome controller, HTTP service or background process.
|
|
6
|
-
|
|
7
|
-
## Project data (target layout, not a production cutover)
|
|
8
|
-
|
|
9
|
-
Each explicitly registered project has one SQLite source of truth:
|
|
10
|
-
~/.chat-loop/context/<projectId>/context.sqlite
|
|
11
|
-
|
|
12
|
-
Optional artifacts/ and backups/ live beside it; no competing project.json,
|
|
13
|
-
current.md, history.md or handoff.md. The user's source repository and AGENTS.md
|
|
14
|
-
are NOT managed or modified by Context. A project may reference authorized
|
|
15
|
-
Workspace roots via Project.sharedContext.workspaceRefs.
|
|
16
|
-
|
|
17
|
-
## SDK method disclosure
|
|
18
|
-
|
|
19
|
-
Use:
|
|
20
|
-
chatloop-context methods list --json
|
|
21
|
-
chatloop-context methods describe project.open
|
|
22
|
-
|
|
23
|
-
Both use lib/methods.mjs, the same catalog that generates ChatGPT MCP inputSchema.
|
|
24
|
-
No hidden run.bind/fence/permit controls are exposed as ordinary SDK methods.
|
|
25
|
-
|
|
26
|
-
Example after an approved pinned installation (future release, not yet published):
|
|
27
|
-
npx --yes --offline --package=@tomflow/chatloop-context@0.1.0 chatloop-context methods list --json
|
|
28
|
-
|
|
29
|
-
Requests take one bounded JSON object from stdin and emit one JSON object on
|
|
30
|
-
stdout. Never place credentials or sensitive project bodies in CLI args or logs:
|
|
31
|
-
echo '{"operation":"sdk.methods.list","group":"project"}' | chatloop-context request --json
|
|
32
|
-
|
|
33
|
-
A trusted local adapter supplies CHAT_LOOP_CONTEXT_ALLOWED_PROJECTS and, if
|
|
34
|
-
needed, CHAT_LOOP_CONTEXT_ROOT (default ~/.chat-loop/context) or an explicit
|
|
35
|
-
CHAT_LOOP_CONTEXT_DB solely for existing legacy Store compatibility.
|
|
36
|
-
The comma-separated Project grant is parsed atomically: invalid, empty or
|
|
37
|
-
duplicate IDs reject the whole grant, including in MCP and Native adapters.
|
|
38
|
-
Use project.list/task.list with limit (1–100) and cursor for bounded pages;
|
|
39
|
-
omitting both keeps the legacy array return. A page is not a cross-request
|
|
40
|
-
snapshot; verify revisions when resuming after concurrent writes.
|
|
41
|
-
|
|
42
|
-
## Reviewed Project registration and Workspace ownership (local operator)
|
|
43
|
-
|
|
44
|
-
The local operator must explicitly enable admin scope, choose the permitted
|
|
45
|
-
Project IDs, and use an **interactive terminal**. The user-reviewed entry
|
|
46
|
-
validates real canonical directories and prompts for an exact action+scope
|
|
47
|
-
confirmation BEFORE creating a Store:
|
|
48
|
-
|
|
49
|
-
CHAT_LOOP_CONTEXT_ADMIN_ENABLE=1 \
|
|
50
|
-
CHAT_LOOP_CONTEXT_ALLOWED_PROJECTS=demo \
|
|
51
|
-
chatloop-context project register demo "Demo project" /confirmed/workspace
|
|
52
|
-
|
|
53
|
-
Additional Workspace roots may be supplied to `register` (up to 16).
|
|
54
|
-
Registration uses a global per-Context-root lock, rejects symlink roots,
|
|
55
|
-
nested/overlapping roots and previously claimed roots, and atomically renames
|
|
56
|
-
a newly staged per-Project SQLite. An existing Project is never overwritten.
|
|
57
|
-
|
|
58
|
-
To replace the **whole** Workspace set on an existing Project, first call
|
|
59
|
-
`project.open` and read its projectRevision and storeEpoch, then explicitly
|
|
60
|
-
run the local operator command (with the same admin grant and TTY confirmation):
|
|
61
|
-
|
|
62
|
-
chatloop-context project bind demo <revision> <storeEpoch> <writeRequestId> /root/a /root/b
|
|
63
|
-
chatloop-context project bind demo <revision> <storeEpoch> <writeRequestId> --clear
|
|
64
|
-
|
|
65
|
-
The second command explicitly clears every Workspace binding. A bind checks
|
|
66
|
-
the original Project identity, CAS revision/epoch, global Project ownership,
|
|
67
|
-
path stability, active execution/Handoff writer fences, then writes one durable
|
|
68
|
-
Core receipt. If the CLI result is lost, use the **same** request ID for a
|
|
69
|
-
read-only operator lookup, never repeat the mutation with a different ID:
|
|
70
|
-
|
|
71
|
-
chatloop-context project receipt demo <storeEpoch> <writeRequestId>
|
|
72
|
-
|
|
73
|
-
The older `project init` remains an explicit *local-admin* compatibility
|
|
74
|
-
command for already approved automated setup; it does **not** itself collect
|
|
75
|
-
a real user-review prompt and must not be offered as the normal first-use flow.
|
|
76
|
-
None of these commands are exposed as ordinary model Context MCP mutations.
|
|
77
|
-
A local TTY/OS-UID operator approval is not cryptographic ChatGPT user
|
|
78
|
-
authentication. Until an independently verified ChatGPT→operator approval
|
|
79
|
-
channel exists, do not claim a completed in-Chat self-service onboarding or
|
|
80
|
-
promote this local workflow into production authorization. None of these
|
|
81
|
-
operations grant Chrome Owner or migrate an existing Legacy Store.
|
|
82
|
-
|
|
83
|
-
## Offline shared Store extraction (diagnostic ONLY, not a cutover)
|
|
84
|
-
|
|
85
|
-
Before a cutover, use the **read-only** source integrity audit with an explicitly
|
|
86
|
-
granted Project and a verified old Store epoch:
|
|
87
|
-
|
|
88
|
-
CHAT_LOOP_CONTEXT_ALLOWED_PROJECTS=demo \
|
|
89
|
-
chatloop-context project audit-sources demo /private/old/shared.sqlite <sourceEpoch>
|
|
90
|
-
|
|
91
|
-
To inspect a Registry task document that changed since shadow scan, point to
|
|
92
|
-
a verified private Recovery Archive copy of the **original** document:
|
|
93
|
-
|
|
94
|
-
chatloop-context project inspect-reference demo /private/old/shared.sqlite \
|
|
95
|
-
<sourceEpoch> <sourceUnitId> <taskId> /private/archive/task-document.md
|
|
96
|
-
|
|
97
|
-
This is a read-only two-version evidence check: exact Registry SHA and
|
|
98
|
-
task identity, archived document SHA, current document SHA, and a bounded
|
|
99
|
-
insertions-only vs changed-content comparison. It outputs a review-required
|
|
100
|
-
receipt (not a stored approval); `canActivate` and `canExecute` remain false.
|
|
101
|
-
Never rewrite the old Registry to suppress drift.
|
|
102
|
-
|
|
103
|
-
**Reviewed local reconciliation (not SourceUnit activation):** after independently
|
|
104
|
-
reviewing the exact original archive, current document, Project scope and old
|
|
105
|
-
writers, a local terminal operator may run:
|
|
106
|
-
|
|
107
|
-
CHAT_LOOP_CONTEXT_ADMIN_ENABLE=1 CHAT_LOOP_CONTEXT_ALLOWED_PROJECTS=demo \
|
|
108
|
-
chatloop-context project reconcile-reference demo /private/old/shared.sqlite \
|
|
109
|
-
<epoch> <sourceUnitId> <migrationRevision> <taskId> <originalArchiveFile> \
|
|
110
|
-
<currentDocSha256> <operatorReviewRef> <writeRequestId>
|
|
111
|
-
|
|
112
|
-
A real TTY is required and the exact source/old/new hashes and review reference
|
|
113
|
-
must be confirmed. Only insertions-only Task document drift is admitted; other
|
|
114
|
-
content changes need separate review. The Core uses epoch, migration revision
|
|
115
|
-
and durable request receipts, keeps original and current SHA plus original
|
|
116
|
-
archive locator in the SourceUnit, leaves the Registry file unchanged and
|
|
117
|
-
keeps the SourceUnit in SHADOW_RO. It cannot create R/P approvals or Browser
|
|
118
|
-
Owner, or assert the independent old-writer / user-authentication gates.
|
|
119
|
-
For an uncertain result, **query** with the same ID, never resubmit with a new ID:
|
|
120
|
-
|
|
121
|
-
chatloop-context project reconcile-receipt demo /private/old/shared.sqlite \
|
|
122
|
-
<epoch> <writeRequestId>
|
|
123
|
-
|
|
124
|
-
This interface is a local-operator candidate; a TTY under one OS UID is not
|
|
125
|
-
proof of a platform-authenticated ChatGPT user approval, so it MUST NOT be used
|
|
126
|
-
by an autonomous agent to authorize production cutover.
|
|
127
|
-
The audit reads source bytes and Registry-linked Task documents, checks exact
|
|
128
|
-
Project Material hash/path coverage, and distinguishes a changed Registry
|
|
129
|
-
file from drift in a linked task document. It reports mixed CURRENT items
|
|
130
|
-
still requiring human classification, without revealing historical document
|
|
131
|
-
bodies or treating a recovered user Turn as fresh approval. Its `BLOCKED`
|
|
132
|
-
status intentionally includes independent old-writer fencing and release/E2E
|
|
133
|
-
gates that a local file scan cannot prove. It never creates a Store or
|
|
134
|
-
modifies SourceUnits, revisions, permissions, Owner or approvals.
|
|
135
|
-
|
|
136
|
-
**Mixed CURRENT EvidenceOnly classification (local operator only):** when
|
|
137
|
-
the complete, exact historical Markdown bytes are already stored as a
|
|
138
|
-
`LEGACY_UNVERIFIED` Project Material under the canonical source path/hash,
|
|
139
|
-
the operator may explicitly classify a mixed source as historical evidence
|
|
140
|
-
without promoting ANY text to a current Project/Task/R/P fact:
|
|
141
|
-
|
|
142
|
-
CHAT_LOOP_CONTEXT_ADMIN_ENABLE=1 CHAT_LOOP_CONTEXT_ALLOWED_PROJECTS=demo \
|
|
143
|
-
chatloop-context project classify-evidence demo /private/old/shared.sqlite \
|
|
144
|
-
<epoch> <sourceUnitId> <migrationRevision> <sourceSha256> \
|
|
145
|
-
<operatorReviewRef> <writeRequestId>
|
|
146
|
-
|
|
147
|
-
The preflight verifies the physical SourceUnit and the complete stored body,
|
|
148
|
-
and requires exact interactive terminal confirmation. Only the Core's
|
|
149
|
-
`migrationClassify` mutates the SourceUnit manifest with
|
|
150
|
-
`taskContexts=[]`, `projectSharedContext={}` and a reference to the
|
|
151
|
-
entire original content as EvidenceOnly/UNKNOWN; the Unit stays SHADOW_RO.
|
|
152
|
-
This is an intentional human-reviewed historical *disposition*, not a
|
|
153
|
-
claim that the historical statements are current or approved. It does not
|
|
154
|
-
activate a Store, release an old writer, or authorize Chrome Owner.
|
|
155
|
-
For an unknown result use `project migration-receipt` with original epoch and
|
|
156
|
-
writeRequestId. Local TTY is not ChatGPT platform user authentication.
|
|
157
|
-
|
|
158
|
-
An older single SQLite may contain several Projects. Never copy or rename it
|
|
159
|
-
directly into the active per-Project location: doing so leaks unrelated data,
|
|
160
|
-
reuses the old epoch and risks reviving stale runs. The local operator can
|
|
161
|
-
first inspect the legacy Store read-only to obtain the **exact** source
|
|
162
|
-
`storeEpoch` and target `projectRevision`, then, on a separate private
|
|
163
|
-
target root and with explicit real TTY confirmation, use:
|
|
164
|
-
|
|
165
|
-
CHAT_LOOP_CONTEXT_ROOT=/private/review-target \\
|
|
166
|
-
CHAT_LOOP_CONTEXT_ALLOWED_PROJECTS=demo \\
|
|
167
|
-
CHAT_LOOP_CONTEXT_ADMIN_ENABLE=1 \\
|
|
168
|
-
chatloop-context project stage-diagnostic demo /private/old/shared.sqlite <sourceEpoch> <projectRevision>
|
|
169
|
-
|
|
170
|
-
This action reads the source through a SQLite online snapshot, selects ONLY
|
|
171
|
-
the named Project and its scoped rows, retains their exact Task/Material/
|
|
172
|
-
SourceUnit history and old-epoch receipts, then strips runnable leases,
|
|
173
|
-
permits and writer credentials. It vacuums foreign Project content from the
|
|
174
|
-
new database and checks the physical row counts, integrity and ownership
|
|
175
|
-
before atomically publishing the result. A private `migration-diagnostic.json`
|
|
176
|
-
receipt names the old/new epochs and counts but is **not** a second active
|
|
177
|
-
Context authority or a retained full-Store backup.
|
|
178
|
-
|
|
179
|
-
The resulting Project is always `DIAGNOSTIC_ONLY`, with a new epoch,
|
|
180
|
-
`canExecute=false` and `approvalsRevalidated=false`. Old receipts cannot
|
|
181
|
-
replay under the new epoch. No SourceUnit becomes `MANAGED_RW`, and no
|
|
182
|
-
Task/Run, browser Owner or previously observed R/P approval gains authority.
|
|
183
|
-
The source is left untouched. An invalid SourceUnit, unexpected schema,
|
|
184
|
-
stale revision, missing operator approval or existing target fails closed;
|
|
185
|
-
incomplete staging is removed. This command is not exposed through MCP.
|
|
186
|
-
|
|
187
|
-
**Production remains BLOCKED** until the physical source hashes, mixed
|
|
188
|
-
CURRENT ownership, old writers and true operator approval have been reviewed,
|
|
189
|
-
old app/Native/Registry clients fenced, fresh backup plus recovery tested and
|
|
190
|
-
the release/real Chrome E2E gates passed. Do not use this diagnostic receipt
|
|
191
|
-
as cutover authorization. See the P0-02 gate in the existing
|
|
192
|
-
`docs/research/chat-loop-context-cli-architecture-20261109.md`.
|
|
193
|
-
|
|
194
|
-
## Safety
|
|
195
|
-
|
|
196
|
-
Writes use expectedStoreEpoch, revision CAS and durable writeRequestId.
|
|
197
|
-
Unknown results must be checked with the SAME request ID; never blindly
|
|
198
|
-
resubmit with another ID. Active-run holder controls live outside ordinary
|
|
199
|
-
MCP/CLI mutations in the restricted Worker→Native adapter. An SDK business
|
|
200
|
-
checkpoint never grants Browser Owner. One OS UID is not per-Chat isolation.
|
|
201
|
-
|
|
202
|
-
No serve, Socket, HTTP API, second Tunnel or per-project daemon. The real
|
|
203
|
-
legacy Context and UNKNOWN Chrome Owner remain unchanged until explicit
|
|
204
|
-
migration and recovery approval.
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// One invocation = one bounded Context SDK request; never a background server.
|
|
3
|
-
import {randomUUID,createHash} from 'node:crypto';
|
|
4
|
-
import {existsSync,mkdirSync,mkdtempSync,renameSync,rmSync} from 'node:fs';
|
|
5
|
-
import {join} from 'node:path';
|
|
6
|
-
let canonicalWorkspaceRefs,confirmWorkspaceOperator,assertWorkspaceUnclaimed,
|
|
7
|
-
withWorkspaceRegistryLock,rebindProjectWorkspaces,readOperatorWorkspaceReceipt;
|
|
8
|
-
import {CONTEXT_CONTRACT,CONTEXT_PACKAGE_VERSION,methodById,validateContextCall,listMethods,describeMethod} from '../lib/methods.mjs';
|
|
9
|
-
// Catalog is Node 20-compatible; SQLite operations load only after Node 24 preflight.
|
|
10
|
-
let openContextProtocol,dispatchContextCall,contextRoot,allowedProjects,
|
|
11
|
-
verifySourceUnit,
|
|
12
|
-
createProjectDiagnosticBranch,auditLegacyProjectSources,
|
|
13
|
-
inspectLegacyRegistryReference;
|
|
14
|
-
const fail=code=>Object.assign(new Error(code),{code});
|
|
15
|
-
const requestLimit=5*1024*1024;
|
|
16
|
-
const output=o=>process.stdout.write(JSON.stringify({
|
|
17
|
-
contract:CONTEXT_CONTRACT,packageVersion:CONTEXT_PACKAGE_VERSION,...o
|
|
18
|
-
})+'\n');
|
|
19
|
-
async function stdinJson(){
|
|
20
|
-
let bytes=0,parts=[];
|
|
21
|
-
for await(const chunk of process.stdin){
|
|
22
|
-
bytes+=chunk.length;
|
|
23
|
-
if(bytes>requestLimit)throw fail('REQUEST_TOO_LARGE');
|
|
24
|
-
parts.push(chunk);
|
|
25
|
-
}
|
|
26
|
-
if(!bytes)throw fail('REQUEST_REQUIRED');
|
|
27
|
-
try{return JSON.parse(Buffer.concat(parts).toString('utf8'))}
|
|
28
|
-
catch{throw fail('INVALID_JSON')}
|
|
29
|
-
}
|
|
30
|
-
async function registerProject(projectId,title,workspaces=[],reviewed=false){
|
|
31
|
-
if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
|
|
32
|
-
if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
|
|
33
|
-
if(!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(projectId))throw fail('INVALID_SCOPE');
|
|
34
|
-
if(typeof title!=='string'||!title.trim()||title.length>200)throw fail('INVALID_TITLE');
|
|
35
|
-
if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
|
|
36
|
-
const refs=canonicalWorkspaceRefs(workspaces,{allowEmpty:!reviewed});
|
|
37
|
-
const root=contextRoot(),final=join(root,projectId);
|
|
38
|
-
if(existsSync(final))throw fail('PROJECT_EXISTS');
|
|
39
|
-
// The user-reviewed entry never creates a Store (or even a directory)
|
|
40
|
-
// before the local operator confirms the exact canonical roots.
|
|
41
|
-
if(reviewed)await confirmWorkspaceOperator('REGISTER',projectId,0,refs);
|
|
42
|
-
mkdirSync(root,{recursive:true,mode:0o700});
|
|
43
|
-
return withWorkspaceRegistryLock(root,()=>{
|
|
44
|
-
let tmp;
|
|
45
|
-
try{
|
|
46
|
-
if(existsSync(final))throw fail('PROJECT_EXISTS');
|
|
47
|
-
if(reviewed){
|
|
48
|
-
const current=canonicalWorkspaceRefs(workspaces);
|
|
49
|
-
if(JSON.stringify(current)!==JSON.stringify(refs))throw fail('WORKSPACE_CHANGED');
|
|
50
|
-
assertWorkspaceUnclaimed(root,projectId,refs);
|
|
51
|
-
}
|
|
52
|
-
tmp=mkdtempSync(join(root,'.register-'));
|
|
53
|
-
const db=openContextProtocol(join(tmp,'context.sqlite'),{
|
|
54
|
-
allowCreate:true,authorizeData:()=>true,principalId:'local-operator'});
|
|
55
|
-
let result;
|
|
56
|
-
try{
|
|
57
|
-
result=db.projectCreate({projectId,title,sharedContext:{workspaceRefs:refs},
|
|
58
|
-
expectedStoreEpoch:db.epoch,writeRequestId:randomUUID()});
|
|
59
|
-
}finally{db.close()}
|
|
60
|
-
renameSync(tmp,final);tmp=null;
|
|
61
|
-
return {...result,workspaceRefs:refs,projectDirectory:final};
|
|
62
|
-
}finally{
|
|
63
|
-
// Incomplete staging is diagnostic only, never silently promoted.
|
|
64
|
-
if(tmp)rmSync(tmp,{recursive:true,force:true});
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
const args=process.argv.slice(2);
|
|
69
|
-
try{
|
|
70
|
-
const node24=Number(process.versions.node.split('.')[0])>=24;
|
|
71
|
-
if(!node24&&args[0]!=='methods')throw fail('CONTEXT_RUNTIME_UNSUPPORTED');
|
|
72
|
-
if(node24){
|
|
73
|
-
({openContextProtocol}=await import('../lib/context-protocol.mjs'));
|
|
74
|
-
({verifySourceUnit}=await import('../lib/context-migration.mjs'));
|
|
75
|
-
({dispatchContextCall,contextRoot,allowedProjects}=await import('../lib/operations.mjs'));
|
|
76
|
-
({canonicalWorkspaceRefs,confirmWorkspaceOperator,assertWorkspaceUnclaimed,
|
|
77
|
-
withWorkspaceRegistryLock,rebindProjectWorkspaces,readOperatorWorkspaceReceipt}=
|
|
78
|
-
await import('../lib/workspace-operator.mjs'));
|
|
79
|
-
({createProjectDiagnosticBranch,auditLegacyProjectSources,
|
|
80
|
-
inspectLegacyRegistryReference}=
|
|
81
|
-
await import('../lib/project-diagnostic.mjs'));
|
|
82
|
-
}
|
|
83
|
-
const [command,sub,...rest]=args;
|
|
84
|
-
let value;
|
|
85
|
-
if(command==='methods'&&sub==='list'&&rest.length<=1)
|
|
86
|
-
value=listMethods(rest[0]==='--json'?undefined:rest[0]);
|
|
87
|
-
else if(command==='methods'&&sub==='describe'&&rest.length===1)
|
|
88
|
-
value=describeMethod(rest[0]);
|
|
89
|
-
else if(command==='request'&&sub==='--json'&&rest.length===0){
|
|
90
|
-
const payload=await stdinJson();
|
|
91
|
-
if(!payload||typeof payload!=='object'||Array.isArray(payload))throw fail('INVALID_ARGUMENT');
|
|
92
|
-
const method=validateContextCall(
|
|
93
|
-
payload.operation&&['sdk.methods.list','sdk.methods.describe'].includes(payload.operation)?
|
|
94
|
-
'chat_loop_context_get':
|
|
95
|
-
// A method is always assigned a fixed read/write mode by the SDK Catalog.
|
|
96
|
-
methodById.get(payload.operation)?.mode==='write'?
|
|
97
|
-
'chat_loop_context_mutate':'chat_loop_context_get',payload);
|
|
98
|
-
value=await dispatchContextCall(method.mode==='write'?
|
|
99
|
-
'chat_loop_context_mutate':'chat_loop_context_get',payload);
|
|
100
|
-
}else if(command==='project'&&sub==='init'&&(rest.length===2||rest.length===3))
|
|
101
|
-
value=await registerProject(rest[0],rest[1],rest.slice(2));
|
|
102
|
-
else if(command==='project'&&sub==='register'&&rest.length>=3&&rest.length<=18)
|
|
103
|
-
value=await registerProject(rest[0],rest[1],rest.slice(2),true);
|
|
104
|
-
else if(command==='project'&&sub==='bind'&&rest.length>=4&&rest.length<=20){
|
|
105
|
-
const [projectId,revision,epoch,requestId,...paths]=rest;
|
|
106
|
-
if(paths.length===1&&paths[0]==='--clear')paths.length=0;
|
|
107
|
-
value=await rebindProjectWorkspaces({projectId,
|
|
108
|
-
expectedProjectRevision:Number(revision),expectedStoreEpoch:epoch,
|
|
109
|
-
writeRequestId:requestId,workspaces:paths});
|
|
110
|
-
}else if(command==='project'&&sub==='receipt'&&rest.length===3)
|
|
111
|
-
value=await readOperatorWorkspaceReceipt({projectId:rest[0],
|
|
112
|
-
storeEpoch:rest[1],writeRequestId:rest[2]});
|
|
113
|
-
else if(command==='project'&&sub==='reconcile-reference'&&rest.length===10){
|
|
114
|
-
const [projectId,sourceDb,expectedStoreEpoch,sourceUnitId,revision,
|
|
115
|
-
taskId,originalArchivePath,expectedCurrentHash,reviewRef,writeRequestId]=rest;
|
|
116
|
-
if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
|
|
117
|
-
if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
|
|
118
|
-
if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
|
|
119
|
-
const expectedMigrationRevision=Number(revision);
|
|
120
|
-
if(!Number.isSafeInteger(expectedMigrationRevision)||expectedMigrationRevision<1)
|
|
121
|
-
throw fail('MIGRATION_VERSION_CONFLICT');
|
|
122
|
-
// Fail closed before any writable handle exists. Review is NOT user R/P.
|
|
123
|
-
const evidence=inspectLegacyRegistryReference({sourceDb,projectId,
|
|
124
|
-
expectedStoreEpoch,sourceUnitId,taskId,archiveFile:originalArchivePath});
|
|
125
|
-
if(evidence.changeKind!=='INSERTIONS_ONLY'||
|
|
126
|
-
evidence.currentTaskHash!==expectedCurrentHash)
|
|
127
|
-
throw fail('REGISTRY_REFERENCE_VERSION_CONFLICT');
|
|
128
|
-
await confirmWorkspaceOperator('RECONCILE_REFERENCE',projectId,
|
|
129
|
-
expectedMigrationRevision,[sourceDb,sourceUnitId,taskId,
|
|
130
|
-
evidence.archivedTaskHash,expectedCurrentHash,reviewRef]);
|
|
131
|
-
const db=openContextProtocol(sourceDb,{principalId:'local-operator',
|
|
132
|
-
authorizeData:(_mode,p)=>p===projectId,
|
|
133
|
-
authorizeAdmin:(op,p)=>p===projectId&&[
|
|
134
|
-
'migration.reference.reconcile',
|
|
135
|
-
'migration.reference.reconcile.reviewed'].includes(op)});
|
|
136
|
-
try{
|
|
137
|
-
value=db.migrationReconcileReference({projectId,sourceUnitId,
|
|
138
|
-
expectedMigrationRevision,expectedStoreEpoch,writeRequestId,
|
|
139
|
-
taskId,originalArchivePath,expectedCurrentHash,
|
|
140
|
-
sourceHash:evidence.sourceHash,reviewRef});
|
|
141
|
-
}finally{db.close()}
|
|
142
|
-
}else if(command==='project'&&['reconcile-receipt','migration-receipt'].includes(sub)&&rest.length===4){
|
|
143
|
-
const [projectId,sourceDb,storeEpoch,writeRequestId]=rest;
|
|
144
|
-
if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
|
|
145
|
-
const db=openContextProtocol(sourceDb,{readOnly:true,
|
|
146
|
-
principalId:'local-operator',
|
|
147
|
-
authorizeData:(_mode,p)=>p===projectId});
|
|
148
|
-
try{value=db.requestStatus({projectId,storeEpoch,writeRequestId})}
|
|
149
|
-
finally{db.close()}
|
|
150
|
-
}else if(command==='project'&&sub==='classify-evidence'&&rest.length===8){
|
|
151
|
-
const [projectId,sourceDb,expectedStoreEpoch,sourceUnitId,revision,
|
|
152
|
-
sourceHash,reviewRef,writeRequestId]=rest;
|
|
153
|
-
if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
|
|
154
|
-
if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
|
|
155
|
-
if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
|
|
156
|
-
const expectedMigrationRevision=Number(revision);
|
|
157
|
-
if(!Number.isSafeInteger(expectedMigrationRevision)||expectedMigrationRevision<1)
|
|
158
|
-
throw fail('MIGRATION_VERSION_CONFLICT');
|
|
159
|
-
if(typeof reviewRef!=='string'||reviewRef.length<8||reviewRef.length>256)
|
|
160
|
-
throw fail('REVIEW_REFERENCE_REQUIRED');
|
|
161
|
-
// The historical body's original bytes must already be a matching
|
|
162
|
-
// LEGACY_UNVERIFIED Project Material, never a synthesized current Task.
|
|
163
|
-
const reader=openContextProtocol(sourceDb,{readOnly:true,
|
|
164
|
-
principalId:'local-operator',authorizeData:(_mode,p)=>p===projectId,
|
|
165
|
-
authorizeAdmin:(op,p)=>op==='migration.list'&&p===projectId});
|
|
166
|
-
let unit;
|
|
167
|
-
try{
|
|
168
|
-
if(reader.mode!=='ACTIVE')throw fail('STORE_DIAGNOSTIC_ONLY');
|
|
169
|
-
if(reader.epoch!==expectedStoreEpoch)throw fail('STALE_EPOCH');
|
|
170
|
-
unit=reader.migrationList(projectId).find(x=>x.sourceUnitId===sourceUnitId);
|
|
171
|
-
if(!unit||unit.status!=='SHADOW_RO'||
|
|
172
|
-
unit.manifest.sourceKind!=='MIXED_CONTEXT')
|
|
173
|
-
throw fail('MIXED_SOURCE_NOT_SHADOW');
|
|
174
|
-
if(unit.migrationRevision!==expectedMigrationRevision)
|
|
175
|
-
throw fail('MIGRATION_VERSION_CONFLICT');
|
|
176
|
-
if(unit.sourceHash!==sourceHash)throw fail('SOURCE_CHANGED');
|
|
177
|
-
verifySourceUnit(unit.manifest);
|
|
178
|
-
const material=reader.projectMaterialList(projectId).filter(m=>
|
|
179
|
-
m.sourcePath===unit.sourcePath&&m.contentHash===sourceHash&&
|
|
180
|
-
m.provenance==='LEGACY_UNVERIFIED');
|
|
181
|
-
if(!material.some(m=>createHash('sha256').update(
|
|
182
|
-
reader.projectMaterialGet(projectId,m.materialId,m.version).body
|
|
183
|
-
).digest('hex')===sourceHash))throw fail('MIGRATION_BODY_NOT_IMPORTED');
|
|
184
|
-
}finally{reader.close()}
|
|
185
|
-
// Human-reviewed disposition: preserve FULL mixed history as
|
|
186
|
-
// EvidenceOnly/UNKNOWN. Do not assert any current Task or R/P fact.
|
|
187
|
-
await confirmWorkspaceOperator('CLASSIFY_EVIDENCE_ONLY',projectId,
|
|
188
|
-
expectedMigrationRevision,[sourceDb,sourceUnitId,sourceHash,reviewRef]);
|
|
189
|
-
const db=openContextProtocol(sourceDb,{principalId:'local-operator',
|
|
190
|
-
authorizeData:(_mode,p)=>p===projectId,
|
|
191
|
-
authorizeAdmin:(op,p)=>p===projectId&&
|
|
192
|
-
['migration.classify','migration.classify.reviewed'].includes(op)});
|
|
193
|
-
try{
|
|
194
|
-
value=db.migrationClassify({projectId,sourceUnitId,sourceHash,
|
|
195
|
-
expectedStoreEpoch,expectedMigrationRevision,writeRequestId,
|
|
196
|
-
classification:{projectId,sourceUnitId,sourceHash,
|
|
197
|
-
currentFactsComplete:true,reviewRef,taskContexts:[],
|
|
198
|
-
projectSharedContext:{},
|
|
199
|
-
unknownHistoryRefs:[{locator:unit.sourcePath,sourceHash}]}});
|
|
200
|
-
}finally{db.close()}
|
|
201
|
-
}else if(command==='project'&&sub==='inspect-reference'&&rest.length===6){
|
|
202
|
-
const [projectId,sourceDb,expectedStoreEpoch,sourceUnitId,taskId,archiveFile]=rest;
|
|
203
|
-
if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
|
|
204
|
-
value=inspectLegacyRegistryReference({projectId,sourceDb,
|
|
205
|
-
expectedStoreEpoch,sourceUnitId,taskId,archiveFile});
|
|
206
|
-
}else if(command==='project'&&sub==='audit-sources'&&rest.length===3){
|
|
207
|
-
const [projectId,sourceDb,expectedStoreEpoch]=rest;
|
|
208
|
-
if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
|
|
209
|
-
value=auditLegacyProjectSources({projectId,sourceDb,expectedStoreEpoch});
|
|
210
|
-
}else if(command==='project'&&sub==='stage-diagnostic'&&rest.length===4){
|
|
211
|
-
const [projectId,sourceDb,expectedStoreEpoch,revision]=rest;
|
|
212
|
-
if(process.env.CHAT_LOOP_CONTEXT_ADMIN_ENABLE!=='1')throw fail('ADMIN_REQUIRED');
|
|
213
|
-
if(!allowedProjects().has(projectId))throw fail('PROJECT_ACCESS_DENIED');
|
|
214
|
-
if(process.env.CHAT_LOOP_CONTEXT_DB)throw fail('EXPLICIT_DB_INIT_REQUIRED');
|
|
215
|
-
const expectedProjectRevision=Number(revision);
|
|
216
|
-
if(!Number.isSafeInteger(expectedProjectRevision)||expectedProjectRevision<1)
|
|
217
|
-
throw fail('PROJECT_VERSION_CONFLICT');
|
|
218
|
-
// This is an offline diagnostic extract; NEVER a source cutover.
|
|
219
|
-
// The operator must review the exact source path and source CAS identity.
|
|
220
|
-
await confirmWorkspaceOperator('STAGE_DIAGNOSTIC',projectId,
|
|
221
|
-
expectedProjectRevision,[sourceDb,expectedStoreEpoch]);
|
|
222
|
-
const root=contextRoot();
|
|
223
|
-
mkdirSync(root,{recursive:true,mode:0o700});
|
|
224
|
-
value=await withWorkspaceRegistryLock(root,()=>createProjectDiagnosticBranch({
|
|
225
|
-
sourceDb,targetRoot:root,projectId,expectedStoreEpoch,expectedProjectRevision
|
|
226
|
-
}));
|
|
227
|
-
}else if(command==='doctor'&&sub==='--json'&&rest.length===0)
|
|
228
|
-
value={status:'READY',packageVersion:CONTEXT_PACKAGE_VERSION,projectRoot:contextRoot()};
|
|
229
|
-
else throw fail('UNKNOWN_COMMAND');
|
|
230
|
-
output({ok:true,result:value});
|
|
231
|
-
}catch(error){
|
|
232
|
-
// Never expose arbitrary OS/SQLite exception messages, paths or user data.
|
|
233
|
-
const code=error.code||'CONTEXT_FAILED';
|
|
234
|
-
output({ok:false,error:{code:/^[A-Z0-9_]+$/.test(code)?code:'CONTEXT_FAILED'}});
|
|
235
|
-
process.exitCode=1;
|
|
236
|
-
}
|