@tomflow/chatloop-extension 0.3.77 → 0.3.79

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.
@@ -179,7 +179,9 @@ async function run(){
179
179
  const evidence=await chrome.tabs.sendMessage(existing.id,
180
180
  {type:repair?'CHAT_LOOP_REPAIR_EVIDENCE_V1':'CHAT_LOOP_APPROVAL_EVIDENCE_V1',
181
181
  ...(taken.explicitChatId
182
- ?{explicitChatId:taken.explicitChatId}:{})}).catch(()=>null);
182
+ ?{explicitChatId:taken.explicitChatId}:{}),
183
+ ...(taken.expectedTaskId
184
+ ?{expectedTaskId:taken.expectedTaskId}:{})}).catch(()=>null);
183
185
  if(evidence?.contract!=='chat-loop.turn-observer.v1'
184
186
  ||evidence.status!=='VERIFIED'||evidence.page!==taken.page
185
187
  ||evidence.userKey!==taken.userKey
@@ -200,7 +200,7 @@ async function resourceChatGate(message,sender) {
200
200
  return {status:'DENIED',code:'RESOURCE_CHAT_LIMIT'};
201
201
  return {status:'OK',code:'RESOURCE_CHAT_REGISTERED'};
202
202
  }
203
- async function approvedAdoptionTarget(chatUrl=null){
203
+ async function approvedAdoptionTarget(chatUrl=null,taskId=null){
204
204
  const requestedId=chatUrl?chatIdOf(chatUrl):null;
205
205
  let tabs=await chrome.tabs.query({url:"https://chatgpt.com/*"});
206
206
  // App and desktop users keep their exact conversation ID. When Chrome
@@ -227,7 +227,8 @@ async function approvedAdoptionTarget(chatUrl=null){
227
227
  }
228
228
  const proof=await chrome.tabs.sendMessage(opened.id,
229
229
  {type:"CHAT_LOOP_APPROVAL_EVIDENCE_V1",
230
- explicitChatId:requestedId}).catch(()=>null);
230
+ explicitChatId:requestedId,
231
+ ...(taskId?{expectedTaskId:taskId}:{})}).catch(()=>null);
231
232
  if(proof?.contract==="chat-loop.turn-observer.v1"
232
233
  &&proof.status==="VERIFIED"
233
234
  &&chatIdOf('https://chatgpt.com'+proof.page)===requestedId){
@@ -244,9 +245,14 @@ async function approvedAdoptionTarget(chatUrl=null){
244
245
  const page=pathOf(tab.url);
245
246
  if(!page||!Number.isSafeInteger(tab.id)
246
247
  ||(requestedId && chatIdOf(tab.url)!==requestedId))continue;
248
+ // No chatUrl means the caller does not authorize opening any Chat.
249
+ // For the approved Task only, inspect each ALREADY OPEN tab by its own
250
+ // immutable Chat ID and require exact native R/P issue-scoped user Turns.
251
+ const scopedId=requestedId||taskId&&chatIdOf(tab.url);
247
252
  const proof=await chrome.tabs.sendMessage(tab.id,
248
253
  {type:"CHAT_LOOP_APPROVAL_EVIDENCE_V1",
249
- ...(requestedId?{explicitChatId:requestedId}:{})}).catch(()=>null);
254
+ ...(scopedId?{explicitChatId:scopedId}:{}),
255
+ ...(!requestedId&&taskId?{expectedTaskId:taskId}:{})}).catch(()=>null);
250
256
  if(proof?.contract!=="chat-loop.turn-observer.v1"
251
257
  ||proof.status!=="VERIFIED"||proof.page!==page
252
258
  ||typeof proof.userKey!=="string"||!/^user:[^\s]{8,}$/.test(proof.userKey)
@@ -255,7 +261,8 @@ async function approvedAdoptionTarget(chatUrl=null){
255
261
  continue;
256
262
  matched.push({tabId:tab.id,page,userKey:proof.userKey,
257
263
  priorAssistantKey:proof.priorAssistantKey||null,
258
- rVersion:proof.rVersion,pVersion:proof.pVersion});
264
+ rVersion:proof.rVersion,pVersion:proof.pVersion,
265
+ ...(!requestedId&&taskId?{explicitChatId:scopedId,expectedTaskId:taskId}:{})});
259
266
  }
260
267
  if(matched.length!==1)return {error:matched.length
261
268
  ?"ADOPTION_APPROVAL_AMBIGUOUS":"ADOPTION_APPROVAL_NOT_FOUND"};
@@ -263,7 +270,9 @@ async function approvedAdoptionTarget(chatUrl=null){
263
270
  await new Promise(resolve=>setTimeout(resolve,400));
264
271
  const second=await chrome.tabs.sendMessage(target.tabId,
265
272
  {type:"CHAT_LOOP_APPROVAL_EVIDENCE_V1",
266
- ...(requestedId?{explicitChatId:requestedId}:{})}).catch(()=>null);
273
+ ...((requestedId||target.explicitChatId)
274
+ ?{explicitChatId:requestedId||target.explicitChatId}:{}),
275
+ ...(target.expectedTaskId?{expectedTaskId:target.expectedTaskId}:{})}).catch(()=>null);
267
276
  if(second?.status!=="VERIFIED"
268
277
  ||second.page!==target.page||second.userKey!==target.userKey
269
278
  ||second.rVersion!==target.rVersion||second.pVersion!==target.pVersion
@@ -379,7 +388,8 @@ async function sessionStartPrepare(message,sender){
379
388
  const adoption=intent.mode==='adopt'
380
389
  ?intent.purpose==='REPAIR_SAME_SCOPE'
381
390
  ?await approvedRepairTarget(prior,intent)
382
- :await approvedAdoptionTarget(intent.chatUrl||null):null;
391
+ :await approvedAdoptionTarget(intent.chatUrl||null,
392
+ intent.contextLeaseRef?intent.taskId:null):null;
383
393
  if(adoption?.error)return {status:'DENIED',code:adoption.error};
384
394
  // A Project-scoped Chat cannot safely hand off to the ordinary home route:
385
395
  // reject BEFORE claiming Owner instead of failing after four hours.
@@ -407,7 +417,10 @@ async function sessionStartPrepare(message,sender){
407
417
  ...(matchingBlocked?{replacedBlockedSessionId:replace}:{}),
408
418
  ...(adoption?{mode:'adopt',adoptTabId:adoption.target.tabId,
409
419
  adoptPage:adoption.target.page,adoptUserKey:adoption.target.userKey,
410
- ...(intent.chatUrl?{adoptExplicitChatId:chatIdOf(intent.chatUrl)}:{}),
420
+ ...((intent.chatUrl||adoption.target.explicitChatId)
421
+ ?{adoptExplicitChatId:chatIdOf(intent.chatUrl)||adoption.target.explicitChatId}:{}),
422
+ ...(adoption.target.expectedTaskId
423
+ ?{adoptExpectedTaskId:adoption.target.expectedTaskId}:{}),
411
424
  adoptPriorAssistantKey:adoption.target.priorAssistantKey,
412
425
  ...(intent.purpose==='REPAIR_SAME_SCOPE'?{}:{
413
426
  adoptRVersion:adoption.target.rVersion,
@@ -446,6 +459,7 @@ async function sessionStartTake(message,sender){
446
459
  ...(run.mode==='adopt'?{mode:'adopt',tabId:run.adoptTabId,
447
460
  page:run.adoptPage,userKey:run.adoptUserKey,
448
461
  ...(run.adoptExplicitChatId?{explicitChatId:run.adoptExplicitChatId}:{}),
462
+ ...(run.adoptExpectedTaskId?{expectedTaskId:run.adoptExpectedTaskId}:{}),
449
463
  priorAssistantKey:run.adoptPriorAssistantKey,
450
464
  ...(run.purpose==='REPAIR_SAME_SCOPE'
451
465
  ?{purpose:'REPAIR_SAME_SCOPE',repairUserText:run.repairUserText,
@@ -477,7 +491,9 @@ async function sessionStartResult(message,sender){
477
491
  const proof=await chrome.tabs.sendMessage(run.adoptTabId,
478
492
  {type:repair?'CHAT_LOOP_REPAIR_EVIDENCE_V1':'CHAT_LOOP_APPROVAL_EVIDENCE_V1',
479
493
  ...(run.adoptExplicitChatId
480
- ?{explicitChatId:run.adoptExplicitChatId}:{})}).catch(()=>null);
494
+ ?{explicitChatId:run.adoptExplicitChatId}:{}),
495
+ ...(run.adoptExpectedTaskId
496
+ ?{expectedTaskId:run.adoptExpectedTaskId}:{})}).catch(()=>null);
481
497
  confirmed=proof?.contract==='chat-loop.turn-observer.v1'
482
498
  &&proof.status==='VERIFIED'&&proof.page===run.adoptPage
483
499
  &&proof.userKey===run.adoptUserKey
@@ -513,6 +529,7 @@ async function sessionStartResult(message,sender){
513
529
  delete next.adoptPage;
514
530
  delete next.adoptUserKey;
515
531
  delete next.adoptExplicitChatId;
532
+ delete next.adoptExpectedTaskId;
516
533
  delete next.adoptPriorAssistantKey;
517
534
  delete next.adoptRVersion;
518
535
  delete next.adoptPVersion;
@@ -561,6 +561,15 @@
561
561
  // Prefer an explicit current-version declaration. Historical mentions
562
562
  // must not choose a version; ambiguous prose is not approval evidence.
563
563
  const versionOf=(value,type)=>{
564
+ // Real formal reviews may start with a status recap mentioning an
565
+ // older P2 before the issued heading "P3 v3|正式修订方案".
566
+ // A unique, version-consistent line title outranks historical prose.
567
+ const issued=[...value.matchAll(new RegExp("(?:^|\\n)\\s*(?:#{1,6}\\s*)?("+type+"[1-9][0-9]*)\\s+v([1-9][0-9]*)\\s*[||]", "gi"))]
568
+ .filter(x=>Number(x[1].slice(1))===Number(x[2]))
569
+ .map(x=>x[1].toUpperCase());
570
+ const issuedVersions=[...new Set(issued)];
571
+ if(issuedVersions.length===1)return issuedVersions[0];
572
+ if(issuedVersions.length>1)return null;
564
573
  const declared=[...value.matchAll(new RegExp("(?:^|\\n)\\s*(?:#{1,6}\\s*)?(?:方案版本|需求版本|当前版本|版本)?[:: ]*("+type+"[1-9][0-9]*)\\b", "gi"))].map(x=>x[1].toUpperCase());
565
574
  const current=[...value.matchAll(new RegExp("(?:本次|当前|本版)[:: ]*("+type+"[1-9][0-9]*)\\b", "gi"))].map(x=>x[1].toUpperCase());
566
575
  const versions=[...new Set(current.length?current:declared.length?declared:(value.match(new RegExp("\\b"+type+"[1-9][0-9]*\\b","gi"))||[]).map(x=>x.toUpperCase()))];
@@ -589,23 +598,52 @@
589
598
  }
590
599
  const rIndex=rows.slice(0,lastUser-1).findLastIndex(x=>x.role==="user"
591
600
  &&confirms(textOf(x),"R"));
592
- if(rIndex<1 ||rows[rIndex-1].role!=="assistant"){
601
+ // Long chats virtualize old turns. A still-mounted native R2 user
602
+ // approval at index 0 has its exact issued R2-ISSUE identity, even when
603
+ // the original R2 assistant has scrolled out of the DOM window.
604
+ // This fallback is ONLY for native issue-scoped approvals. The Native
605
+ // Context Start lease independently verifies the effective R approval.
606
+ const rUser=rIndex>=0?rows[rIndex]:null;
607
+ const nativeR=rUser&&textOf(rUser).match(/^[A-Z][A-Z0-9._:-]{7,120}[||]\s*(R[1-9][0-9]*)-ISSUE:[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}[||]\s*我确认\s+(R[1-9][0-9]*)\s+v([1-9][0-9]*)[。.!!]/i);
608
+ const nativeRVersion=nativeR
609
+ &&nativeR[1].toUpperCase()===nativeR[2].toUpperCase()
610
+ &&Number(nativeR[1].slice(1))===Number(nativeR[3])
611
+ ?nativeR[1].toUpperCase():null;
612
+ const rAssistant=rIndex>0?rows[rIndex-1]:null;
613
+ if(rIndex<0 || rIndex===0&&!nativeRVersion
614
+ ||rIndex>0&&rAssistant?.role!=="assistant"){
593
615
  fail("APPROVAL_REQUIREMENTS_MISSING");return false;
594
616
  }
595
- const rAssistant=rows[rIndex-1],rUser=rows[rIndex];
617
+ if(message.expectedTaskId!==undefined){
618
+ // A no-URL idle adoption is only eligible for its exact Managed
619
+ // Task's TWO native issued approval messages in the same DOM Chat.
620
+ // No standalone historical affirmations or copied Task summaries.
621
+ const task=message.expectedTaskId;
622
+ const nativeIssue=(row,type)=>typeof task==="string"
623
+ &&/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(task)
624
+ &&textOf(row).startsWith(task+"|")
625
+ &&new RegExp("^"+type+"[1-9][0-9]*-ISSUE:"
626
+ +"[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}[||]\\s*我确认\\s+"
627
+ +type+"[1-9][0-9]*\\s+v[1-9][0-9]*","i")
628
+ .test(textOf(row).slice(task.length+1));
629
+ if(!nativeRVersion||!nativeIssue(rUser,"R")
630
+ ||!nativeIssue(pUser,"P")){
631
+ fail("APPROVAL_TASK_SCOPE_MISMATCH");return false;
632
+ }
633
+ }
596
634
  // Only document-level TEST_ONLY markers deny production adoption:
597
635
  // normal P plans can legitimately mention test-only scenarios.
598
636
  const testOnlyReview=x=>
599
637
  /(?:^|\n)\s*(?:#{1,6}\s*|>\s*)?(?:\*\*)?TEST_ONLY(?:\*\*)?(?:\s*$|\s*[/||:]\s*NOT_APPROVED)/mi
600
638
  .test(textOf(x));
601
- if(testOnlyReview(rAssistant)||testOnlyReview(pAssistant)){
639
+ if(rAssistant&&testOnlyReview(rAssistant)||testOnlyReview(pAssistant)){
602
640
  fail("APPROVAL_TEST_ONLY");return false;
603
641
  }
604
- if(!/(?:需求|范围|验收)/.test(textOf(rAssistant))
605
- || !/\bR[1-9][0-9]*\b/i.test(textOf(rAssistant))) {
642
+ if(rAssistant&&(!/(?:需求|范围|验收)/.test(textOf(rAssistant))
643
+ || !/\bR[1-9][0-9]*\b/i.test(textOf(rAssistant)))) {
606
644
  fail("APPROVAL_REQUIREMENTS_MISSING");return false;
607
645
  }
608
- const rVersion=versionOf(textOf(rAssistant),"R");
646
+ const rVersion=rAssistant?versionOf(textOf(rAssistant),"R"):nativeRVersion;
609
647
  const intervening=rows.slice(rIndex+1,lastUser-1);
610
648
  const invalidated=intervening.some(x=>{
611
649
  const text=textOf(x);
@@ -613,8 +651,10 @@
613
651
  const next=versionOf(text,"R");
614
652
  return /需求|范围|验收/.test(text)&&next&&next!==rVersion;
615
653
  }
616
- return /(?:撤销|取消|拒绝|不同意|不批准|修改|修订|变更)/.test(text)
617
- && /需求|R[1-9][0-9]*/i.test(text);
654
+ // "修订 P3、保留 R2" is a plan update, NOT an R2 revocation.
655
+ // Only an explicit requirement-targeted change invalidates this R.
656
+ return /(?:撤销|取消|拒绝|不同意|不批准|修改|修订|变更)\s*(?:(?:当前|本次|这份|上述)\s*)?(?:需求|R[1-9][0-9]*)/i.test(text)
657
+ || /\bR[1-9][0-9]*\b\s*(?:需求)?\s*(?:需要|应当|应该)?\s*(?:撤销|取消|拒绝|修改|修订|变更)/i.test(text);
618
658
  });
619
659
  if(!pVersion||!rVersion||invalidated){
620
660
  fail("APPROVAL_REVISED_OR_REVOKED");return false;
@@ -656,7 +696,20 @@
656
696
  const explicitChatId=typeof message.explicitChatId==="string"
657
697
  ? message.explicitChatId.toLowerCase():null;
658
698
  const currentId=path.match(/\/c\/([0-9a-f-]{12,})$/i)?.[1].toLowerCase();
659
- const scopedIdle=Boolean(explicitChatId && explicitChatId===currentId
699
+ // The managed lease may scope an idle, already-open Chat by the exact
700
+ // Task ID in BOTH real native R/P approval turns. No task scope from
701
+ // copied Context prose or generic unscoped historical confirmations.
702
+ const issueTask=x=>textOf(x).match(
703
+ /^([A-Z][A-Z0-9._:-]{7,120})[||]\s*[RP][1-9][0-9]*-ISSUE:[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}[||]/i)?.[1]||null;
704
+ const taskScoped=typeof message.expectedTaskId==="string"
705
+ &&message.expectedTaskId.length>=8
706
+ &&issueTask(rUser)===message.expectedTaskId
707
+ &&issueTask(pUser)===message.expectedTaskId;
708
+ if(message.expectedTaskId!==undefined&&!taskScoped){
709
+ fail("APPROVAL_TASK_SCOPE_MISMATCH");return false;
710
+ }
711
+ const scopedIdle=Boolean(
712
+ (explicitChatId&&explicitChatId===currentId||taskScoped)
660
713
  &&!snap.stop&&!snap.busy&&snap.assistantKey
661
714
  &&snap.composerReady&&snap.copyAction&&snap.text
662
715
  &&!snap.pendingConfirmation);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "Chat Loop Extension",
4
- "version": "0.3.77",
4
+ "version": "0.3.79",
5
5
  "description": "Chat Loop browser companion: Eye actions, Turn observer, Continue and Handoff Actions",
6
6
  "permissions": [
7
7
  "activeTab",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomflow/chatloop-extension",
3
- "version": "0.3.77",
3
+ "version": "0.3.79",
4
4
  "type": "module",
5
5
  "description": "Chat Loop Extension package: nine formal Action pages and its own native CLI bridge",
6
6
  "bin": {