@tomflow/chatloop-extension 0.3.59 → 0.3.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -5
- package/bin/native-host.mjs +26 -1
- package/extension/actions/continue.js +35 -14
- package/extension/actions/handoff.js +79 -25
- package/extension/actions/start.js +11 -3
- package/extension/background.js +456 -29
- package/extension/loop/observer.js +46 -0
- package/extension/manifest.json +1 -1
- package/lib/bridge.mjs +17 -1
- package/lib/context-ipc.mjs +65 -0
- package/lib/session-file.mjs +6 -3
- package/package.json +1 -1
|
@@ -453,6 +453,52 @@
|
|
|
453
453
|
userKey: observation.userKey});
|
|
454
454
|
return false;
|
|
455
455
|
}
|
|
456
|
+
if (message?.type === "CHAT_LOOP_REPAIR_EVIDENCE_V1") {
|
|
457
|
+
// Read-only proof of a NEW repair request in the EXACT Chat whose
|
|
458
|
+
// previous assistant finished with machine DONE. Not a new R/P approval.
|
|
459
|
+
const path=conversationPath();
|
|
460
|
+
const mains=[...document.querySelectorAll("main")].filter(visible);
|
|
461
|
+
const fail=reason=>reply({contract:CONTRACT,status:"UNVERIFIED",reason});
|
|
462
|
+
if(!path||mains.length!==1){fail("REPAIR_CHAT_UNVERIFIED");return false;}
|
|
463
|
+
const main=mains[0];
|
|
464
|
+
const legacy=[...main.querySelectorAll(
|
|
465
|
+
'[data-message-author-role="user"],[data-message-author-role="assistant"]')];
|
|
466
|
+
const nodes=legacy.length?legacy:[...main.querySelectorAll(
|
|
467
|
+
'[data-chatgpt-search-unit-key][data-chatgpt-search-message-ids]')];
|
|
468
|
+
const role=el=>el.getAttribute("data-message-author-role")
|
|
469
|
+
||el.getAttribute("data-chatgpt-search-unit-key")?.split(":").at(-1);
|
|
470
|
+
const userIdx=nodes.findLastIndex(el=>role(el)==="user");
|
|
471
|
+
if(userIdx<1||role(nodes[userIdx-1])!=="assistant"){
|
|
472
|
+
fail("REPAIR_SEQUENCE_UNVERIFIED");return false;
|
|
473
|
+
}
|
|
474
|
+
const text=el=>(el.querySelector?.(".markdown")?.innerText
|
|
475
|
+
||el.innerText||el.textContent||"").trim();
|
|
476
|
+
const lastUser=nodes[userIdx],priorAssistant=nodes[userIdx-1];
|
|
477
|
+
const userText=text(lastUser).replace(/\s+/g," ");
|
|
478
|
+
if(userText.length<4||userText.length>2000
|
|
479
|
+
||!/(?:修复|修正|返工|修改|补完|不通过|重新做|继续完成|改好|fix|repair|rework|correct)/i.test(userText)
|
|
480
|
+
||/(?:不要|不用|无需|拒绝).{0,8}(?:修复|修改|继续|fix|repair)/i.test(userText)
|
|
481
|
+
||!/\[CHAT_LOOP_STATUS:DONE\]\s*$/.test(text(priorAssistant))){
|
|
482
|
+
fail("REPAIR_EXPLICIT_INTENT_NOT_FOUND");return false;
|
|
483
|
+
}
|
|
484
|
+
const snap=inspect(),userKey=messageId(lastUser,"user"),
|
|
485
|
+
doneAssistantKey=messageId(priorAssistant,"assistant");
|
|
486
|
+
const requestedId=typeof message.explicitChatId==="string"
|
|
487
|
+
?message.explicitChatId.toLowerCase():null;
|
|
488
|
+
const currentId=path.match(/\/c\/([0-9a-f-]{12,})$/i)?.[1].toLowerCase();
|
|
489
|
+
if(requestedId&&requestedId!==currentId){
|
|
490
|
+
fail("REPAIR_CHAT_ID_CHANGED");return false;
|
|
491
|
+
}
|
|
492
|
+
if(!userKey||snap.path!==path||snap.userKey!==userKey
|
|
493
|
+
||snap.fatal||snap.dialog||snap.toolUnknown
|
|
494
|
+
||!(snap.stop||snap.busy||requestedId
|
|
495
|
+
&&!snap.pendingConfirmation&&snap.composerReady)){
|
|
496
|
+
fail("REPAIR_LAST_USER_UNVERIFIED");return false;
|
|
497
|
+
}
|
|
498
|
+
reply({contract:CONTRACT,status:"VERIFIED",page:path,
|
|
499
|
+
userKey,userText,priorDoneAssistantKey:doneAssistantKey});
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
456
502
|
if (message?.type === "CHAT_LOOP_APPROVAL_EVIDENCE_V1") {
|
|
457
503
|
// Read-only, fail-closed two-stage approval proof for same-Chat adoption.
|
|
458
504
|
// An explicit model assertion or a copied task Context is never evidence.
|
package/extension/manifest.json
CHANGED
package/lib/bridge.mjs
CHANGED
|
@@ -27,8 +27,18 @@ export function requestDir(id) {
|
|
|
27
27
|
function validateIntent(action, intent) {
|
|
28
28
|
if(action==='start'){
|
|
29
29
|
if(!intent||typeof intent!=='object'||Array.isArray(intent)
|
|
30
|
-
||Object.keys(intent).some(k=>!['projectId','prompt','confirmed','replaceBlockedSessionId','mode','chatUrl'].includes(k))
|
|
30
|
+
||Object.keys(intent).some(k=>!['projectId','taskId','purpose','prompt','confirmed','replaceBlockedSessionId','mode','chatUrl','contextLeaseRef','contextStoreEpoch','contextTaskRevision','contextApprovalRevision'].includes(k))
|
|
31
31
|
||!(/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/).test(intent.projectId||'')
|
|
32
|
+
||(intent.taskId!==undefined && !(/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/).test(intent.taskId))
|
|
33
|
+
||(intent.purpose!==undefined && (intent.purpose!=='REPAIR_SAME_SCOPE'
|
|
34
|
+
||intent.mode!=='adopt'||!intent.taskId||!intent.contextLeaseRef))
|
|
35
|
+
||(['contextLeaseRef','contextStoreEpoch','contextTaskRevision','contextApprovalRevision']
|
|
36
|
+
.some(k=>Object.hasOwn(intent,k))
|
|
37
|
+
&&(!intent.taskId||typeof intent.contextLeaseRef!=='string'
|
|
38
|
+
||intent.contextLeaseRef.length<8
|
|
39
|
+
||typeof intent.contextStoreEpoch!=='string'||intent.contextStoreEpoch.length<10
|
|
40
|
+
||!Number.isSafeInteger(intent.contextTaskRevision)||intent.contextTaskRevision<1
|
|
41
|
+
||!Number.isSafeInteger(intent.contextApprovalRevision)||intent.contextApprovalRevision<1))
|
|
32
42
|
||intent.confirmed!==true || (intent.mode!==undefined && intent.mode!=='adopt')
|
|
33
43
|
|| (intent.replaceBlockedSessionId!==undefined
|
|
34
44
|
&& !/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(intent.replaceBlockedSessionId))
|
|
@@ -40,6 +50,12 @@ function validateIntent(action, intent) {
|
|
|
40
50
|
||/[\x00-\x08\x0b\x0c\x0e-\x1f]/.test(intent.prompt)))
|
|
41
51
|
throw Object.assign(Error('Explicit confirmed task and project required'),{code:'INVALID_START_INTENT'});
|
|
42
52
|
return {projectId:intent.projectId,confirmed:true,
|
|
53
|
+
...(intent.taskId?{taskId:intent.taskId}:{}),
|
|
54
|
+
...(intent.purpose?{purpose:intent.purpose}:{}),
|
|
55
|
+
...(intent.contextLeaseRef?{contextLeaseRef:intent.contextLeaseRef,
|
|
56
|
+
contextStoreEpoch:intent.contextStoreEpoch,
|
|
57
|
+
contextTaskRevision:intent.contextTaskRevision,
|
|
58
|
+
contextApprovalRevision:intent.contextApprovalRevision}:{}),
|
|
43
59
|
...(intent.mode==='adopt'?{mode:'adopt',...(intent.chatUrl?{chatUrl:intent.chatUrl}:{})}:{prompt:intent.prompt}),
|
|
44
60
|
...(intent.replaceBlockedSessionId?{replaceBlockedSessionId:intent.replaceBlockedSessionId}:{})};
|
|
45
61
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Browser Native Host -> existing Local Tools Supervisor narrow IPC.
|
|
2
|
+
// This file deliberately does not import Local Tools internals or create
|
|
3
|
+
// another Context service. A same-UID shell remains within the trusted local boundary.
|
|
4
|
+
import {createHmac,randomUUID} from 'node:crypto';
|
|
5
|
+
import {connect} from 'node:net';
|
|
6
|
+
import {existsSync,lstatSync,readFileSync} from 'node:fs';
|
|
7
|
+
import {homedir} from 'node:os';
|
|
8
|
+
import {join} from 'node:path';
|
|
9
|
+
const fail=code=>Object.assign(new Error(code),{code});
|
|
10
|
+
const base=()=>process.env.CHAT_LOOP_LOCAL_TOOLS_STATE_DIR||
|
|
11
|
+
join(homedir(),'.chat-loop','local-tools');
|
|
12
|
+
function privateFile(path,type){
|
|
13
|
+
if(!existsSync(path))throw fail('CONTEXT_CONTROL_NOT_INSTALLED');
|
|
14
|
+
const st=lstatSync(path);
|
|
15
|
+
if((type==='socket'?!st.isSocket():!st.isFile())
|
|
16
|
+
||st.isSymbolicLink()||(st.mode&0o077)
|
|
17
|
+
||(process.getuid && st.uid!==process.getuid()))
|
|
18
|
+
throw fail('CONTEXT_CONTROL_UNTRUSTED');
|
|
19
|
+
return st;
|
|
20
|
+
}
|
|
21
|
+
const ipc=(request,timeout=4000)=>new Promise((resolve,reject)=>{
|
|
22
|
+
const socket=connect(join(base(),'control.sock'));
|
|
23
|
+
let text='',done=false;
|
|
24
|
+
const timer=setTimeout(()=>finish(fail('CONTEXT_CONTROL_TIMEOUT')),timeout);
|
|
25
|
+
function finish(error,value){
|
|
26
|
+
if(done)return;done=true;clearTimeout(timer);socket.destroy();
|
|
27
|
+
error?reject(error):resolve(value);
|
|
28
|
+
}
|
|
29
|
+
socket.on('connect',()=>socket.write(JSON.stringify(request)+'\n'));
|
|
30
|
+
socket.on('error',e=>finish(e));
|
|
31
|
+
socket.on('end',()=>{if(!done)finish(fail('CONTEXT_CONTROL_UNCONFIRMED'))});
|
|
32
|
+
socket.on('data',chunk=>{
|
|
33
|
+
text+=chunk.toString();
|
|
34
|
+
if(text.length>1_000_000)return finish(fail('CONTEXT_CONTROL_REPLY_TOO_LARGE'));
|
|
35
|
+
const n=text.indexOf('\n');if(n<0)return;
|
|
36
|
+
try{finish(null,JSON.parse(text.slice(0,n)))}catch{finish(fail('CONTEXT_CONTROL_REPLY_INVALID'))}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
export async function nativeContextControl(command,args){
|
|
40
|
+
const dir=base();
|
|
41
|
+
const st=lstatSync(dir);
|
|
42
|
+
if(!st.isDirectory()||st.isSymbolicLink()||(st.mode&0o077)
|
|
43
|
+
||(process.getuid&&st.uid!==process.getuid()))
|
|
44
|
+
throw fail('CONTEXT_CONTROL_UNTRUSTED');
|
|
45
|
+
privateFile(join(dir,'control.sock'),'socket');
|
|
46
|
+
privateFile(join(dir,'manager.lock'),'file');
|
|
47
|
+
const key=join(dir,'context-control.secret');
|
|
48
|
+
const secretStats=privateFile(key,'file');
|
|
49
|
+
if(secretStats.size!==32)throw fail('CONTEXT_CONTROL_UNTRUSTED');
|
|
50
|
+
const hello=await ipc({action:'__ping'},2000);
|
|
51
|
+
const lock=JSON.parse(readFileSync(join(dir,'manager.lock'),'utf8'));
|
|
52
|
+
if(hello.contract!=='chat-loop.local-control.v1'
|
|
53
|
+
||lock.nonce!==hello.ownerNonce||lock.pid!==hello.managerPid
|
|
54
|
+
||lock.version!==hello.version||hello.version!==hello.packageVersion)
|
|
55
|
+
throw fail('CONTEXT_CONTROL_OWNER_MISMATCH');
|
|
56
|
+
const secret=readFileSync(key);
|
|
57
|
+
const signed={contract:'chat-loop.context-control.v1',
|
|
58
|
+
requestId:randomUUID(),issuedAt:Date.now(),payload:{command,args}};
|
|
59
|
+
const mac=createHmac('sha256',secret).update(JSON.stringify(signed)).digest('hex');
|
|
60
|
+
const reply=await ipc({...signed,action:'context-control',mac},6000);
|
|
61
|
+
if(reply.contract!==hello.contract||reply.requestId!==signed.requestId)
|
|
62
|
+
throw fail('CONTEXT_CONTROL_REPLY_INVALID');
|
|
63
|
+
if(reply.status!=='OK')throw fail(reply.error?.code||'CONTEXT_CONTROL_FAILED');
|
|
64
|
+
return reply.result;
|
|
65
|
+
}
|
package/lib/session-file.mjs
CHANGED
|
@@ -20,6 +20,7 @@ const ensurePrivate=dir=>{
|
|
|
20
20
|
const validate=r=>{
|
|
21
21
|
if(!r||typeof r!=='object'||Array.isArray(r)||
|
|
22
22
|
!ID.test(r.sessionId||'')||!PROJECT.test(r.projectId||'')||
|
|
23
|
+
(r.taskId!==undefined && !PROJECT.test(r.taskId))||
|
|
23
24
|
!PHASE.has(r.phase)||!Number.isSafeInteger(r.generation)||r.generation<0||
|
|
24
25
|
!Number.isSafeInteger(r.updatedAt)||r.updatedAt<=0)
|
|
25
26
|
throw fail('SESSION_INVALID');
|
|
@@ -33,9 +34,9 @@ const validate=r=>{
|
|
|
33
34
|
if(r.activeChatUrl!==null&&!r.activeChatUrl.endsWith('/'+r.activeChatId))
|
|
34
35
|
throw fail('SESSION_CHAT_INVALID');
|
|
35
36
|
if(r.activeChatId===null&&r.phase==='WAITING_REPLY')throw fail('SESSION_CHAT_INVALID');
|
|
36
|
-
const keys=['sessionId','projectId','phase','generation','activeChatId','activeChatUrl','updatedAt'];
|
|
37
|
+
const keys=['sessionId','projectId','phase','generation','activeChatId','activeChatUrl','updatedAt','taskId'];
|
|
37
38
|
if(Object.keys(r).some(k=>!keys.includes(k)))throw fail('SESSION_EXTRA_FIELDS');
|
|
38
|
-
return Object.fromEntries(keys.map(k=>[k,r[k]]));
|
|
39
|
+
return Object.fromEntries(keys.filter(k=>Object.hasOwn(r,k)).map(k=>[k,r[k]]));
|
|
39
40
|
};
|
|
40
41
|
export function readSession(){
|
|
41
42
|
const file=sessionPath();
|
|
@@ -63,6 +64,7 @@ export function writeSession(input,replaceBlockedSessionId=null,resumeBlockedSes
|
|
|
63
64
|
||old.sessionId!==resumeBlockedSessionId
|
|
64
65
|
||next.sessionId!==old.sessionId
|
|
65
66
|
||next.projectId!==old.projectId
|
|
67
|
+
||next.taskId!==old.taskId
|
|
66
68
|
||next.generation!==old.generation
|
|
67
69
|
||next.activeChatId!==old.activeChatId
|
|
68
70
|
||next.activeChatUrl!==old.activeChatUrl
|
|
@@ -78,6 +80,7 @@ export function writeSession(input,replaceBlockedSessionId=null,resumeBlockedSes
|
|
|
78
80
|
throw fail('SESSION_ALREADY_ACTIVE');
|
|
79
81
|
if(old&&old.sessionId===next.sessionId){
|
|
80
82
|
if(next.projectId!==old.projectId)throw fail('SESSION_PROJECT_CHANGED');
|
|
83
|
+
if(next.taskId!==old.taskId)throw fail('SESSION_TASK_CHANGED');
|
|
81
84
|
if(next.generation<old.generation)throw fail('SESSION_STALE_GENERATION');
|
|
82
85
|
if(old.phase==='DONE'&&next.phase!=='DONE'||old.phase==='BLOCKED'
|
|
83
86
|
&& !['BLOCKED','DONE'].includes(next.phase)
|
|
@@ -93,7 +96,7 @@ export function writeSession(input,replaceBlockedSessionId=null,resumeBlockedSes
|
|
|
93
96
|
// transition: preserve Native's CAS timestamp and avoid rewriting disk.
|
|
94
97
|
// A genuine phase/identity/generation change still advances the record.
|
|
95
98
|
if(old && !replaceBlockedSessionId && !resumeBlockedSessionId
|
|
96
|
-
&& ['sessionId','projectId','phase','generation',
|
|
99
|
+
&& ['sessionId','projectId','taskId','phase','generation',
|
|
97
100
|
'activeChatId','activeChatUrl'].every(key=>old[key]===next[key]))
|
|
98
101
|
return old;
|
|
99
102
|
const temp=join(dir,'.active-session-'+randomUUID()+'.tmp');
|