@tomflow/chatloop-extension 0.4.9 → 0.4.11

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.
@@ -1,6 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  // Chrome Native Messaging: one request per process, no persistent server.
3
3
  import {nativeHostApi} from '../index.mjs';
4
+ // Chrome owns this one-shot Native process. Never inherit a shell/CLI trace
5
+ // parent; only the verified private Action request can establish its parent.
6
+ delete process.env.CHAT_LOOP_API_PARENT;
4
7
  const hostContract=nativeHostApi();
5
8
  const {readRequest,finish,readSession,writeSession,registeredContext,
6
9
  nativeContextControl,nativeApiObservation,observeNativeOperation}=hostContract;
@@ -25,7 +28,9 @@ process.stdin.on('data',async chunk=>{
25
28
  if(m.op==='acquire'){
26
29
  const r=readRequest(m.id);
27
30
  if(r.action!==m.action)throw Object.assign(Error('Action mismatch'),{code:'REQUEST_MISMATCH'});
28
- write({status:'OK',id:r.id,action:r.action,nonce:r.nonce,...(r.intent ? {intent:r.intent} : {})});
31
+ write({status:'OK',id:r.id,action:r.action,nonce:r.nonce,
32
+ ...(r.traceParent?{traceParent:r.traceParent}:{}),
33
+ ...(r.intent ? {intent:r.intent} : {})});
29
34
  }else if(m.op==='complete'){
30
35
  const r=finish(m);
31
36
  write({status:'SAVED',id:r.id,action:r.action,bytes:r.bytes});
@@ -71,7 +76,23 @@ process.stdin.on('data',async chunk=>{
71
76
  // The telemetry operation is intentionally excluded from self-recording.
72
77
  // Recording it here would recursively spawn more Native log requests.
73
78
  if(m.op==='api-observation')await dispatch();
74
- else await observeNativeOperation(m.op,dispatch);
79
+ else {
80
+ // Only a private CLI request is allowed to anchor a Native child span.
81
+ // A browser-supplied ID is never treated as authorization or receipt.
82
+ let parent=null;
83
+ try{
84
+ if(['acquire','complete'].includes(m.op)){
85
+ const record=readRequest(m.id);
86
+ if(record.action===m.action)parent=record.traceParent||null;
87
+ }else if(m.op==='session-read'&&m.sourceRequestId){
88
+ const record=readRequest(m.sourceRequestId);
89
+ const p=m.traceParent;
90
+ if(record.action==='status'&&record.traceParent&&p&&
91
+ record.traceParent.traceId===p.traceId)parent=p;
92
+ }
93
+ }catch{/* Non-authoritative parent cannot modify Native business outcome. */}
94
+ await observeNativeOperation(m.op,dispatch,parent);
95
+ }
75
96
  }catch(e){write({status:'FAILED',code:e.code||'NATIVE_HOST_ERROR'});}
76
97
  finally{
77
98
  // Observation-only requests cannot recursively generate Native audit.
@@ -18,7 +18,8 @@ export async function queueModelIntent(action) {
18
18
  const query=typeof auth.intent.intentId==='string'||auth.intent.lookup===true;
19
19
  outcome = await chrome.runtime.sendMessage({
20
20
  type:query?'CHAT_LOOP_MODEL_INTENT_STATUS_V1':'CHAT_LOOP_MODEL_INTENT_V1',
21
- id, action:action.toUpperCase(),intent:auth.intent
21
+ id, action:action.toUpperCase(),intent:auth.intent,
22
+ ...(auth.traceParent?{traceParent:auth.traceParent}:{})
22
23
  });
23
24
  if(outcome?.status!==(query?'STATUS':'QUEUED')
24
25
  || !/^[a-f0-9]{32}$/.test(outcome.intentId||'')
@@ -180,10 +180,12 @@ async function run(){
180
180
  ||typeof proof.nonce!=='string')throw Error(proof?.code||'START_NATIVE_INVALID');
181
181
  let prepared,taken,newTab,committed;
182
182
  try{
183
- prepared=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_PREPARE_V1',requestId:id});
183
+ prepared=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_PREPARE_V1',
184
+ requestId:id,...(proof.traceParent?{traceParent:proof.traceParent}:{})});
184
185
  if(prepared?.status!=='PREPARED')throw Error(prepared?.code||'START_PREPARE_DENIED');
185
186
  taken=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_TAKE_V1',
186
- requestId:id,token:prepared.token});
187
+ requestId:id,token:prepared.token,
188
+ ...(proof.traceParent?{traceParent:proof.traceParent}:{})});
187
189
  if(taken?.status!=='OK'||taken.sessionId!==prepared.sessionId
188
190
  ||(taken.mode==='adopt'
189
191
  ? !Number.isSafeInteger(taken.tabId)||!/^\/c\/[0-9a-f-]{12,}$/i.test(taken.page||'')
@@ -220,6 +222,7 @@ async function run(){
220
222
  // No tabs.create, no composer input, no send-button click in this branch.
221
223
  committed=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
222
224
  requestId:id,token:prepared.token,result:'ADOPTED_CONFIRMED',
225
+ ...(proof.traceParent?{traceParent:proof.traceParent}:{}),
223
226
  newTabId:existing.id,newPage:taken.page,newUserKey:taken.userKey});
224
227
  if(committed?.status!=='OK')throw Error(committed?.code||'ADOPTION_COMMIT_UNVERIFIED');
225
228
  await chrome.tabs.sendMessage(existing.id,{type:'CHAT_LOOP_TURN_RESUME_V1',
@@ -248,7 +251,8 @@ async function run(){
248
251
  await observerReady(newTab.id);
249
252
  const receipt=await sendFirst(newTab.id,taken.prompt);
250
253
  committed=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
251
- requestId:id,token:prepared.token,...receipt,result:receipt.status});
254
+ requestId:id,token:prepared.token,...receipt,result:receipt.status,
255
+ ...(proof.traceParent?{traceParent:proof.traceParent}:{})});
252
256
  if(committed?.status!=='OK')throw Error(committed?.code||'START_COMMIT_UNVERIFIED');
253
257
  const stored=await chrome.runtime.sendNativeMessage(host,{op:'complete',
254
258
  id,nonce:proof.nonce,action:'start',status:'OK',payload:JSON.stringify(committed)});
@@ -268,6 +272,7 @@ async function run(){
268
272
  .includes(e.message) ? e.message : null;
269
273
  await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_START_RESULT_V1',
270
274
  requestId:id,token:prepared.token,result:'UNVERIFIED',newTabId:newTab?.id,
275
+ ...(proof.traceParent?{traceParent:proof.traceParent}:{}),
271
276
  ...(failureCode?{failureCode}:{})}).catch(()=>{});
272
277
  }
273
278
  const failed=await chrome.runtime.sendNativeMessage(host,{op:'complete',id,nonce:proof.nonce,
@@ -8,7 +8,8 @@ async function run(){
8
8
  const proof=await chrome.runtime.sendNativeMessage(host,{op:'acquire',id,action:'status'});
9
9
  if(proof?.status!=='OK'||proof.action!=='status'||proof.id!==id)
10
10
  throw Error(proof?.code||'STATUS_NATIVE_INVALID');
11
- const state=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_STATUS_V1',requestId:id});
11
+ const state=await chrome.runtime.sendMessage({type:'CHAT_LOOP_SESSION_STATUS_V1',
12
+ requestId:id,...(proof.traceParent?{traceParent:proof.traceParent}:{})});
12
13
  const receipt=await chrome.runtime.sendNativeMessage(host,{op:'complete',id,
13
14
  nonce:proof.nonce,action:'status',status:'OK',payload:JSON.stringify(state)});
14
15
  if(receipt?.status!=='SAVED')throw Error(receipt?.code||'STATUS_NOT_STORED');
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "Chat Loop Extension",
4
- "version": "0.4.9",
4
+ "version": "0.4.11",
5
5
  "description": "Chat Loop browser companion: Eye actions, Turn observer, Continue and Handoff Actions",
6
6
  "permissions": [
7
7
  "activeTab",
@@ -106,7 +106,9 @@ async function sessionStatus(message,sender){
106
106
  if(!sessionPage(sender,'status',message.requestId))
107
107
  return {status:'DENIED',code:'INVALID_STATUS_SOURCE'};
108
108
  const run=await readRun();
109
- const disk=await chrome.runtime.sendNativeMessage(SESSION_HOST,{op:'session-read'}).catch(()=>null);
109
+ const disk=await chrome.runtime.sendNativeMessage(SESSION_HOST,{op:'session-read',
110
+ ...(message.auditDispatchParent?{sourceRequestId:message.requestId,
111
+ traceParent:message.auditDispatchParent}:{})}).catch(()=>null);
110
112
  if(disk?.status!=='OK')return {status:'UNVERIFIED',code:'SESSION_STATE_UNVERIFIED'};
111
113
  const projection=run?.sessionId?sessionProjection(run):null;
112
114
  if(projection&&!matchesNativeProjection(projection,disk.session))
@@ -71,13 +71,14 @@ const SERIAL_ROUTES=new Map([
71
71
  ['CHAT_LOOP_RESOURCE_CHAT_V1',resourceChatGate]
72
72
 
73
73
  ]);
74
- const onBrowserMessage=(message, sender, reply) => {
74
+ const onBrowserMessage=(message, sender, reply, dispatchParent=null) => {
75
75
  const type=message?.type;
76
76
  const handle=SERIAL_ROUTES.get(type);
77
77
  if(handle){
78
78
  void serial(async()=>{
79
79
  let result;
80
- try{result=await handle(message,sender);}
80
+ try{result=await handle(type==='CHAT_LOOP_SESSION_STATUS_V1'
81
+ ?{...message,auditDispatchParent:dispatchParent}:message,sender);}
81
82
  catch(error){result={status:'BLOCKED',code:error.message||'SCHEDULER_ERROR'};}
82
83
  // A bounded diagnostic receipt, never permission or a second state store.
83
84
  // In particular a rejected manual recovery must be observable remotely.
@@ -156,8 +157,25 @@ chrome.runtime.onMessage.addListener((message,sender,reply)=>{
156
157
  }catch{/* Reply telemetry cannot change the browser callback. */}
157
158
  }
158
159
  };
160
+ // Only the known, exact status Action may offer an AOP parent. The
161
+ // existing sessionPage/Owner gate still independently checks the request.
162
+ const sourceId=message?.requestId||message?.id;
163
+ const actionSource=/^[a-f0-9]{32}$/.test(sourceId||'')&&(
164
+ type==='CHAT_LOOP_SESSION_STATUS_V1'&&
165
+ sender?.url===chrome.runtime.getURL('status.html')+'?requestId='+sourceId||
166
+ ['CHAT_LOOP_MODEL_INTENT_V1','CHAT_LOOP_MODEL_INTENT_STATUS_V1']
167
+ .includes(type)&&['CONTINUE','HANDOFF'].includes(message?.action)&&
168
+ sender?.url===chrome.runtime.getURL(message.action.toLowerCase()+'.html')+
169
+ '?requestId='+sourceId||
170
+ ['CHAT_LOOP_SESSION_START_PREPARE_V1','CHAT_LOOP_SESSION_START_TAKE_V1',
171
+ 'CHAT_LOOP_SESSION_START_RESULT_V1'].includes(type)&&
172
+ sender?.url===chrome.runtime.getURL('start.html')+'?requestId='+sourceId);
159
173
  return workerApi.audit.observe('browser.dispatch.'+type,
160
- onBrowserMessage,'browser',{
174
+ // Read dispatchSpan only when the AOP invokes its target: onBegin has
175
+ // already allocated the child Span. Passing it as an eager argument
176
+ // captures undefined before onBegin and breaks Native parent linkage.
177
+ (msg,src,cb)=>onBrowserMessage(msg,src,cb,dispatchSpan),'browser',{
178
+ ...(actionSource&&message.traceParent?{parent:message.traceParent}:{}),
161
179
  onBegin:ids=>{dispatchSpan=ids;}
162
180
  })(message,sender,respond);
163
181
  });
package/index.mjs CHANGED
@@ -31,7 +31,7 @@ import * as binding from './lib/context-binding.mjs';
31
31
  import * as ipc from './lib/context-ipc.mjs';
32
32
  import * as evidence from './lib/owner-evidence.mjs';
33
33
 
34
- bridge.configureBridgeActions(actionCapabilities);
34
+ bridge.configureBridgeActions(actionCapabilities,apiAspect.current);
35
35
  binding.configureContextBinding({configFile:bridge.configFile});
36
36
  evidence.configureOwnerEvidence({
37
37
  readSession:session.readSession,sessionPath:session.sessionPath,
@@ -60,11 +60,12 @@ export const nativeHostApi=apiAspect.observe('nativeHostApi',
60
60
  registeredContext:binding.registeredContext,
61
61
  nativeContextControl:ipc.nativeContextControl,
62
62
  // One named, non-authoritative span for each Native messaging operation.
63
- observeNativeOperation:(op,perform)=>{
63
+ observeNativeOperation:(op,perform,parent=null)=>{
64
64
  if(typeof perform!=='function')throw TypeError('NATIVE_OPERATION_REQUIRED');
65
65
  const name=typeof op==='string'&&/^[a-z][a-z0-9-]{0,60}$/.test(op)
66
66
  ?op:'invalid';
67
- return apiAspect.observe('native.op.'+name,perform,'native')();
67
+ return apiAspect.observe('native.op.'+name,perform,'native',
68
+ parent?{parent}:{})();
68
69
  },
69
70
  nativeApiObservation:ipc.nativeApiObservation
70
71
  }), 'nativeHostApi','native'));
package/lib/bridge.mjs CHANGED
@@ -5,12 +5,17 @@ import {join} from 'node:path';
5
5
 
6
6
  export const HOST_NAME = 'com.tomflow.chatloop.extension';
7
7
  export let ACTIONS=Object.freeze([]);
8
- export function configureBridgeActions(actions){
8
+ let readApiParent=()=>null;
9
+ const API_UUID=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
10
+ const validParent=p=>p&&API_UUID.test(p.traceId||'')&&API_UUID.test(p.spanId||'');
11
+ export function configureBridgeActions(actions,parentReader=()=>null){
9
12
  if(ACTIONS.length)throw Error('EXTENSION_ACTIONS_ALREADY_CONFIGURED');
10
13
  if(!Array.isArray(actions)||!actions.length||
11
14
  actions.some(action=>typeof action?.id!=='string'))
12
15
  throw Error('EXTENSION_ACTIONS_INVALID');
13
16
  ACTIONS=Object.freeze(actions.map(action=>action.id));
17
+ if(typeof parentReader!=='function')throw Error('EXTENSION_TRACE_PORT_INVALID');
18
+ readApiParent=parentReader;
14
19
  }
15
20
  const ID = /^[a-f0-9]{32}$/;
16
21
  const MAX_TEXT = 8 * 1024 * 1024;
@@ -122,7 +127,11 @@ export function prepare(action,intent) {
122
127
  cleanup();
123
128
  const id=randomUUID().replaceAll('-',''), nonce=randomUUID().replaceAll('-',''), at=Date.now();
124
129
  const dir=privateDir(requestDir(id));
125
- writeFileSync(join(dir,'request.json'), JSON.stringify({id,nonce,action,at,expiresAt:at+TTL_MS,...(validated ? {intent:validated} : {})}),
130
+ // This private request is the already-authorized CLI → Native transport.
131
+ // Correlation is soft: never promote it to a capability or Owner proof.
132
+ let traceParent=null;
133
+ try{const current=readApiParent();if(validParent(current))traceParent=current}catch{}
134
+ writeFileSync(join(dir,'request.json'), JSON.stringify({id,nonce,action,at,expiresAt:at+TTL_MS,...(traceParent?{traceParent}:{}),...(validated ? {intent:validated} : {})}),
126
135
  {flag:'wx',mode:0o600});
127
136
  return {id,nonce,action,dir};
128
137
  }
@@ -136,6 +145,7 @@ export function readRequest(id) {
136
145
  if(r.id!==id||!ACTIONS.includes(r.action)||Date.now()>r.expiresAt)
137
146
  throw Object.assign(Error('Request expired or changed'),{code:'REQUEST_EXPIRED'});
138
147
  if(['continue','handoff','clean','start'].includes(r.action))validateIntent(r.action,r.intent);
148
+ if(r.traceParent!==undefined&&!validParent(r.traceParent))delete r.traceParent;
139
149
  return r;
140
150
  }
141
151
  export function finish({id,nonce,action,status,payload='',code=null}) {
@@ -78,9 +78,10 @@ export async function nativeApiObservation(record){
78
78
  Object.keys(record).some(key=>!['callId','traceId','spanId','parentSpanId',
79
79
  'apiName','phase','durationMs','errorCode'].includes(key))||
80
80
  !API_UUID.test(record.callId||'')||
81
- (record.traceId!==undefined&&!API_UUID.test(record.traceId))||
82
- (record.spanId!==undefined&&record.spanId!==record.callId)||
83
- (record.parentSpanId!==undefined&&!API_UUID.test(record.parentSpanId))||
81
+ !API_UUID.test(record.traceId||'')||
82
+ record.spanId!==record.callId||
83
+ (record.parentSpanId!==undefined&&
84
+ (!API_UUID.test(record.parentSpanId)||record.parentSpanId===record.spanId))||
84
85
  !API_NAME.test(record.apiName||'')||
85
86
  !['BEGIN','OK','FAILED'].includes(record.phase)||
86
87
  (record.phase==='BEGIN'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomflow/chatloop-extension",
3
- "version": "0.4.9",
3
+ "version": "0.4.11",
4
4
  "type": "module",
5
5
  "description": "Chat Loop Extension package: nine formal Action pages and its own native CLI bridge",
6
6
  "bin": {
@@ -33,6 +33,6 @@
33
33
  ".": "./index.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@tomflow/chatloop-trace": "0.4.3"
36
+ "@tomflow/chatloop-trace": "0.4.4"
37
37
  }
38
38
  }