@tomflow/chatloop-extension 0.4.3 → 0.4.5

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/bin/cli.mjs CHANGED
@@ -137,4 +137,9 @@ if(help!==null){
137
137
  }
138
138
  // After the CLI reply is produced, allow bounded observation delivery.
139
139
  // This never changes its result or retries its business side effect.
140
- await cliContract[Symbol.for('chat-loop.telemetry.drain.internal')]?.(300);
140
+ const audit=await cliContract[Symbol.for('chat-loop.telemetry.drain.internal')]?.(4200);
141
+ if(audit?.status!=='OK'){
142
+ // Sanitized counters only; the already-emitted RESULT is authoritative.
143
+ process.stderr.write('CHAT_LOOP_AUDIT_GAP '+JSON.stringify({
144
+ component:'extension.cli',...audit})+'\n');
145
+ }
@@ -9,7 +9,7 @@ const write=obj=>{
9
9
  const b=Buffer.from(JSON.stringify(obj)),len=Buffer.alloc(4);
10
10
  len.writeUInt32LE(b.length);process.stdout.write(Buffer.concat([len,b]));
11
11
  };
12
- let buf=Buffer.alloc(0),handled=false;
12
+ let buf=Buffer.alloc(0),handled=false,observationOnly=false;
13
13
  process.stdin.on('data',async chunk=>{
14
14
  if(handled)return;
15
15
  buf=Buffer.concat([buf,chunk]);
@@ -20,6 +20,7 @@ process.stdin.on('data',async chunk=>{
20
20
  handled=true;
21
21
  try{
22
22
  const m=JSON.parse(buf.subarray(4,4+n).toString('utf8'));
23
+ observationOnly=m.op==='api-observation';
23
24
  const dispatch=async()=>{
24
25
  if(m.op==='acquire'){
25
26
  const r=readRequest(m.id);
@@ -73,8 +74,15 @@ process.stdin.on('data',async chunk=>{
73
74
  else await observeNativeOperation(m.op,dispatch);
74
75
  }catch(e){write({status:'FAILED',code:e.code||'NATIVE_HOST_ERROR'});}
75
76
  finally{
76
- // The Native business reply has already been written. Allow the one
77
- // existing Supervisor writer to receive pending soft evidence.
78
- await hostContract[Symbol.for('chat-loop.telemetry.drain.internal')]?.(300);
77
+ // Observation-only requests cannot recursively generate Native audit.
78
+ // Never delay their Writer receipt for an unrelated drain timeout.
79
+ if(!observationOnly){
80
+ const audit=await hostContract[Symbol.for('chat-loop.telemetry.drain.internal')]?.(2500);
81
+ if(audit?.status!=='OK'){
82
+ // The business reply is already written and remains authoritative.
83
+ process.stderr.write('CHAT_LOOP_AUDIT_GAP '+JSON.stringify({
84
+ component:'extension.native',...audit})+'\n');
85
+ }
86
+ }
79
87
  }
80
88
  });
@@ -42,4 +42,29 @@ function createOrderedApiObservationSender(deliver,{parallel=16,capacity=2048}={
42
42
  health:()=>Object.freeze({...health,pending:waiting.length,inFlight:running})});
43
43
  }
44
44
 
45
+ // One bounded CLI/Native process-exit gate, after the business reply.
46
+ // The Trace AOP queue and this transport have separate async backlogs.
47
+ // Never count "queue empty" as "Writer accepted the records".
48
+ function createObservationDrain(aspect,sender){
49
+ if(typeof aspect?.drain!=='function'||typeof aspect?.health!=='function'
50
+ ||typeof sender?.health!=='function')throw TypeError('OBSERVATION_DRAIN_INVALID');
51
+ return async function drain(timeoutMs=4200){
52
+ const budget=Number.isSafeInteger(timeoutMs)
53
+ ?Math.max(0,Math.min(timeoutMs,6000)):4200;
54
+ const deadline=Date.now()+budget;
55
+ let state;
56
+ do{
57
+ const remaining=Math.max(1,deadline-Date.now());
58
+ await aspect.drain(Math.min(750,remaining));
59
+ const a=aspect.health(),s=sender.health();
60
+ state={pending:a.pending+s.pending,inFlight:a.inFlight+s.inFlight,
61
+ dropped:a.dropped+s.dropped,failed:a.sinkFailures+s.failed,
62
+ submitted:s.submitted,confirmed:s.confirmed};
63
+ if(state.pending===0&&state.inFlight===0)break;
64
+ }while(Date.now()<deadline);
65
+ return Object.freeze({...state,status:state.pending||state.inFlight
66
+ ?'TIMEOUT':state.dropped||state.failed?'LOG_GAP':'OK'});
67
+ };
68
+ }
69
+
45
70
  globalThis.ChatLoopApiObservationV1=Object.freeze({createOrderedApiObservationSender});
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "Chat Loop Extension",
4
- "version": "0.4.3",
4
+ "version": "0.4.5",
5
5
  "description": "Chat Loop browser companion: Eye actions, Turn observer, Continue and Handoff Actions",
6
6
  "permissions": [
7
7
  "activeTab",
@@ -114,12 +114,19 @@ const onBrowserMessage=(message, sender, reply) => {
114
114
  throw Error("INVALID_RELOAD_SOURCE");
115
115
  reply({status: "ACCEPTED"});
116
116
  await chrome.tabs.remove(id);
117
- // Reload destroys the MV3 Worker immediately. Its dispatch/reply AOP
118
- // terminal records have already been enqueued after the business reply,
119
- // but async Native transport may still be in flight. Drain only that
120
- // soft evidence before teardown; timeout/failure never retries the
121
- // Action or changes the accepted business result.
122
- try{await workerApi.audit.drain(300)}catch{/* Audit is non-authoritative. */}
117
+ // Reload terminates pending MV3 Native Messaging Promises immediately.
118
+ // Each span's terminal record follows its BEGIN across a one-shot
119
+ // Native process, which can take longer than a single 300-ms drain.
120
+ // The business receipt is already sent; only soft audit transport
121
+ // may run until this bounded teardown deadline (never retry Action).
122
+ try{
123
+ const deadline=Date.now()+2300;
124
+ while(Date.now()<deadline){
125
+ if(await workerApi.audit.drain(Math.min(700,deadline-Date.now())))break;
126
+ const health=workerApi.audit.health();
127
+ if(health.pending===0&&health.inFlight===0)break;
128
+ }
129
+ }catch{/* Audit cannot change the accepted business result. */}
123
130
  chrome.runtime.reload();
124
131
  } catch (error) {reply({status: "FAILED", code: error.message});}
125
132
  })();
package/index.mjs CHANGED
@@ -4,7 +4,7 @@ import {fileURLToPath} from 'node:url';
4
4
  import {actionCapabilities} from './actions.generated.mjs';
5
5
  import {matchExtensionAction} from './lib/action-routing.mjs';
6
6
  import {createApiAspect} from '@tomflow/chatloop-trace';
7
- import {createOrderedApiObservationSender} from './lib/api-observation-queue.mjs';
7
+ import {createOrderedApiObservationSender,createObservationDrain} from './lib/api-observation-queue.mjs';
8
8
  const observationSender=createOrderedApiObservationSender(record=>
9
9
  ipc.nativeApiObservation(record));
10
10
  const INTERNAL_DRAIN=Symbol.for('chat-loop.telemetry.drain.internal');
@@ -24,6 +24,7 @@ const apiAspect=createApiAspect('@tomflow/chatloop-extension',{
24
24
  });
25
25
  }
26
26
  });
27
+ const drainTelemetry=createObservationDrain(apiAspect,observationSender);
27
28
  import * as bridge from './lib/bridge.mjs';
28
29
  import * as session from './lib/session-file.mjs';
29
30
  import * as binding from './lib/context-binding.mjs';
@@ -44,7 +45,7 @@ export const verifyOrphanOwnerIdle=apiAspect.observe(
44
45
  // Internal entrypoint contracts, not a second npm subpath.
45
46
  export const extensionCliApi=apiAspect.observe('extensionCliApi',
46
47
  ()=>apiAspect.methods(Object.freeze({
47
- [INTERNAL_DRAIN]:apiAspect.drain,
48
+ [INTERNAL_DRAIN]:drainTelemetry,
48
49
  ACTIONS:bridge.ACTIONS,actionCapabilities,matchExtensionAction,HOST_NAME:bridge.HOST_NAME,
49
50
  configFile:bridge.configFile,prepare:bridge.prepare,
50
51
  readResult:bridge.readResult,privateDir:bridge.privateDir,
@@ -52,7 +53,7 @@ export const extensionCliApi=apiAspect.observe('extensionCliApi',
52
53
  }), 'extensionCliApi','cli'));
53
54
  export const nativeHostApi=apiAspect.observe('nativeHostApi',
54
55
  ()=>apiAspect.methods(Object.freeze({
55
- [INTERNAL_DRAIN]:apiAspect.drain,
56
+ [INTERNAL_DRAIN]:drainTelemetry,
56
57
  readRequest:bridge.readRequest,finish:bridge.finish,
57
58
  readSession:session.readSession,writeSession:session.writeSession,
58
59
  registeredContext:binding.registeredContext,
@@ -41,3 +41,28 @@ export function createOrderedApiObservationSender(deliver,{parallel=16,capacity=
41
41
  return Object.freeze({send,
42
42
  health:()=>Object.freeze({...health,pending:waiting.length,inFlight:running})});
43
43
  }
44
+
45
+ // One bounded CLI/Native process-exit gate, after the business reply.
46
+ // The Trace AOP queue and this transport have separate async backlogs.
47
+ // Never count "queue empty" as "Writer accepted the records".
48
+ export function createObservationDrain(aspect,sender){
49
+ if(typeof aspect?.drain!=='function'||typeof aspect?.health!=='function'
50
+ ||typeof sender?.health!=='function')throw TypeError('OBSERVATION_DRAIN_INVALID');
51
+ return async function drain(timeoutMs=4200){
52
+ const budget=Number.isSafeInteger(timeoutMs)
53
+ ?Math.max(0,Math.min(timeoutMs,6000)):4200;
54
+ const deadline=Date.now()+budget;
55
+ let state;
56
+ do{
57
+ const remaining=Math.max(1,deadline-Date.now());
58
+ await aspect.drain(Math.min(750,remaining));
59
+ const a=aspect.health(),s=sender.health();
60
+ state={pending:a.pending+s.pending,inFlight:a.inFlight+s.inFlight,
61
+ dropped:a.dropped+s.dropped,failed:a.sinkFailures+s.failed,
62
+ submitted:s.submitted,confirmed:s.confirmed};
63
+ if(state.pending===0&&state.inFlight===0)break;
64
+ }while(Date.now()<deadline);
65
+ return Object.freeze({...state,status:state.pending||state.inFlight
66
+ ?'TIMEOUT':state.dropped||state.failed?'LOG_GAP':'OK'});
67
+ };
68
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomflow/chatloop-extension",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "type": "module",
5
5
  "description": "Chat Loop Extension package: nine formal Action pages and its own native CLI bridge",
6
6
  "bin": {