@thinkrun/cli 0.1.35 → 0.1.36
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 +13 -2
- package/dist/shared/local-audit-sync.js +1 -0
- package/dist/src/adapters/cloud.d.ts.map +1 -1
- package/dist/src/adapters/cloud.js +27 -1
- package/dist/src/adapters/cloud.js.map +1 -1
- package/dist/src/adapters/local.d.ts +18 -7
- package/dist/src/adapters/local.d.ts.map +1 -1
- package/dist/src/adapters/local.js +242 -58
- package/dist/src/adapters/local.js.map +1 -1
- package/dist/src/commands/actions.d.ts +2 -0
- package/dist/src/commands/actions.d.ts.map +1 -1
- package/dist/src/commands/actions.js +176 -43
- package/dist/src/commands/actions.js.map +1 -1
- package/dist/src/commands/audit.d.ts.map +1 -1
- package/dist/src/commands/audit.js +35 -5
- package/dist/src/commands/audit.js.map +1 -1
- package/dist/src/commands/cloud.d.ts.map +1 -1
- package/dist/src/commands/cloud.js +14 -6
- package/dist/src/commands/cloud.js.map +1 -1
- package/dist/src/commands/doctor.d.ts +7 -0
- package/dist/src/commands/doctor.d.ts.map +1 -1
- package/dist/src/commands/doctor.js +72 -9
- package/dist/src/commands/doctor.js.map +1 -1
- package/dist/src/commands/release.d.ts.map +1 -1
- package/dist/src/commands/release.js +303 -23
- package/dist/src/commands/release.js.map +1 -1
- package/dist/src/session/cli-session-sync.d.ts +18 -3
- package/dist/src/session/cli-session-sync.d.ts.map +1 -1
- package/dist/src/session/cli-session-sync.js +188 -31
- package/dist/src/session/cli-session-sync.js.map +1 -1
- package/dist/src/session/context.d.ts +22 -3
- package/dist/src/session/context.d.ts.map +1 -1
- package/dist/src/session/context.js +132 -16
- package/dist/src/session/context.js.map +1 -1
- package/dist/src/working-location.d.ts +52 -4
- package/dist/src/working-location.d.ts.map +1 -1
- package/dist/src/working-location.js +648 -22
- package/dist/src/working-location.js.map +1 -1
- package/package.json +1 -1
|
@@ -11,9 +11,9 @@ import { ApiError, SessionError } from '../errors.js';
|
|
|
11
11
|
import { config } from '../config/store.js';
|
|
12
12
|
import { resolveTabAuditState } from '../../shared/audit-state.js';
|
|
13
13
|
import { readBridgePort, DEFAULT_BRIDGE_PORT, BRIDGE_HEALTH_PROBE_TIMEOUT_MS, BRIDGE_REQUEST_TIMEOUT_MS, unwrapEvaluatePayload, } from '../utils.js';
|
|
14
|
-
import { clearLocalSessionContext, getLocalSessionContext, setLocalSessionContext } from '../session/context.js';
|
|
14
|
+
import { clearLocalSessionContext, getLocalSessionBindingSnapshot, getLocalSessionContext, localSessionBindingAuthorityEquals, setLocalSessionContext } from '../session/context.js';
|
|
15
15
|
import { initCliSession, syncBrowserAction, syncScreenshotAction, closeCliSession, readCliSessionId, readCliNetworkCursor, readCliAuditScreenshotState, writeCliNetworkCursor, writeCliAuditScreenshotState, unrefAbortSignal, } from '../session/cli-session-sync.js';
|
|
16
|
-
import {
|
|
16
|
+
import { canClearContextAfterTargetedReleaseFailure, getDurableWorkingLocation, getLockedBy, getWorkingLocation, releaseWorkingLocationForTab, setWorkingLocation, updateWorkingLocationControlSession, withAgentBindingClaim } from '../working-location.js';
|
|
17
17
|
import { resolveAgentId } from '../session/agent-identity.js';
|
|
18
18
|
import { localCommandRequiresTabError } from '../session/errors.js';
|
|
19
19
|
import { assessLocalContinuity } from '../session/local-continuity.js';
|
|
@@ -149,6 +149,8 @@ export class LocalAdapter {
|
|
|
149
149
|
validatedTabCache = new Map();
|
|
150
150
|
syncedNetworkRequestMarkers = new Map();
|
|
151
151
|
auditScreenshotChains = new Map();
|
|
152
|
+
/** Ownership decisions made while resolving the command target. */
|
|
153
|
+
resolvedTabOwnership = new Map();
|
|
152
154
|
TAB_CACHE_TTL_MS = 60_000;
|
|
153
155
|
/** Injected fetch function — allows unit tests to intercept all HTTP calls. */
|
|
154
156
|
fetchFn;
|
|
@@ -247,7 +249,7 @@ export class LocalAdapter {
|
|
|
247
249
|
console.warn(`[thinkrun] audit screenshot ceiling reached for session ${persistedSessionId}; dropping additional audit frames`);
|
|
248
250
|
return;
|
|
249
251
|
}
|
|
250
|
-
const sessionId = this.
|
|
252
|
+
const sessionId = this.getRequestSessionId();
|
|
251
253
|
const headers = { 'Content-Type': 'application/json' };
|
|
252
254
|
if (sessionId)
|
|
253
255
|
headers['x-session-id'] = sessionId;
|
|
@@ -545,23 +547,65 @@ export class LocalAdapter {
|
|
|
545
547
|
return false;
|
|
546
548
|
}
|
|
547
549
|
try {
|
|
548
|
-
if (workingLocation
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
550
|
+
if (workingLocation) {
|
|
551
|
+
withAgentBindingClaim(() => {
|
|
552
|
+
const durable = getDurableWorkingLocation();
|
|
553
|
+
const currentBinding = getLocalSessionBindingSnapshot(true);
|
|
554
|
+
const bindingMatches = currentBinding.privateTabId === localContext.tabId
|
|
555
|
+
|| currentBinding.sharedTabId === localContext.tabId
|
|
556
|
+
|| currentBinding.activeSessionId === localContext.tabId;
|
|
557
|
+
if (!durable
|
|
558
|
+
|| durable.tabId !== workingLocation.tabId
|
|
559
|
+
|| durable.setAt !== workingLocation.setAt
|
|
560
|
+
|| !bindingMatches
|
|
561
|
+
|| (currentBinding.privateAgentSessionId
|
|
562
|
+
&& localContext.agentSessionId
|
|
563
|
+
&& currentBinding.privateAgentSessionId !== localContext.agentSessionId)
|
|
564
|
+
|| (currentBinding.legacyAgentSessionId
|
|
565
|
+
&& localContext.agentSessionId
|
|
566
|
+
&& currentBinding.legacyAgentSessionId !== localContext.agentSessionId)) {
|
|
567
|
+
throw new Error('TAB_LOCKED: local binding changed before authority repair');
|
|
568
|
+
}
|
|
569
|
+
if (localContext.controlSessionId) {
|
|
570
|
+
updateWorkingLocationControlSession(localContext.controlSessionId, true);
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
setLocalSessionContext(localContext.tabId, localContext.agentSessionId, localContext.windowId, true);
|
|
574
|
+
}
|
|
575
|
+
});
|
|
554
576
|
return true;
|
|
555
577
|
}
|
|
556
578
|
if (!workingLocation) {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
579
|
+
withAgentBindingClaim(() => {
|
|
580
|
+
const durable = getDurableWorkingLocation();
|
|
581
|
+
const currentBinding = getLocalSessionBindingSnapshot(true);
|
|
582
|
+
const bindingStillExists = currentBinding.privateTabId === localContext.tabId
|
|
583
|
+
|| currentBinding.sharedTabId === localContext.tabId
|
|
584
|
+
|| currentBinding.activeSessionId === localContext.tabId;
|
|
585
|
+
if (durable
|
|
586
|
+
|| !bindingStillExists
|
|
587
|
+
|| (currentBinding.privateTabId && currentBinding.privateTabId !== localContext.tabId)
|
|
588
|
+
|| (currentBinding.sharedTabId && currentBinding.sharedTabId !== localContext.tabId)
|
|
589
|
+
|| (currentBinding.privateAgentSessionId
|
|
590
|
+
&& localContext.agentSessionId
|
|
591
|
+
&& currentBinding.privateAgentSessionId !== localContext.agentSessionId)
|
|
592
|
+
|| (currentBinding.privateControlSessionId
|
|
593
|
+
&& localContext.controlSessionId
|
|
594
|
+
&& currentBinding.privateControlSessionId !== localContext.controlSessionId)
|
|
595
|
+
|| (currentBinding.legacyAgentSessionId
|
|
596
|
+
&& localContext.agentSessionId
|
|
597
|
+
&& currentBinding.legacyAgentSessionId !== localContext.agentSessionId)) {
|
|
598
|
+
throw new Error('TAB_LOCKED: local binding changed before authority repair');
|
|
599
|
+
}
|
|
600
|
+
setWorkingLocation({
|
|
601
|
+
tabId: Number(localContext.tabId),
|
|
602
|
+
...(typeof localContext.windowId === 'number' ? { windowId: localContext.windowId } : {}),
|
|
603
|
+
...(localContext.controlSessionId ? { controlSessionId: localContext.controlSessionId } : {}),
|
|
604
|
+
}, true);
|
|
605
|
+
if (!localContext.controlSessionId) {
|
|
606
|
+
setLocalSessionContext(localContext.tabId, localContext.agentSessionId, localContext.windowId, true);
|
|
607
|
+
}
|
|
561
608
|
});
|
|
562
|
-
if (!localContext.controlSessionId) {
|
|
563
|
-
setLocalSessionContext(localContext.tabId, localContext.agentSessionId, localContext.windowId);
|
|
564
|
-
}
|
|
565
609
|
return true;
|
|
566
610
|
}
|
|
567
611
|
}
|
|
@@ -583,6 +627,13 @@ export class LocalAdapter {
|
|
|
583
627
|
if (!loc)
|
|
584
628
|
return; // no lock set — nothing to enforce
|
|
585
629
|
const ctx = getLocalSessionContext();
|
|
630
|
+
let initialBinding;
|
|
631
|
+
try {
|
|
632
|
+
initialBinding = getLocalSessionBindingSnapshot();
|
|
633
|
+
}
|
|
634
|
+
catch (error) {
|
|
635
|
+
this.rethrowTabLock(error);
|
|
636
|
+
}
|
|
586
637
|
const workingTabId = String(loc.tabId);
|
|
587
638
|
if (ctx && workingTabId === ctx.tabId)
|
|
588
639
|
return; // already on the right tab
|
|
@@ -618,51 +669,114 @@ export class LocalAdapter {
|
|
|
618
669
|
}
|
|
619
670
|
}
|
|
620
671
|
catch {
|
|
621
|
-
//
|
|
672
|
+
// Bridge recovery is best-effort; durable correction below still requires
|
|
673
|
+
// that this location remain current.
|
|
622
674
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
const { setLocalSessionContext } = await import('../session/context.js');
|
|
626
|
-
setLocalSessionContext(workingTabId, reboundAgentSessionId, loc.windowId);
|
|
627
|
-
}
|
|
628
|
-
catch { /* ignore */ }
|
|
629
|
-
}
|
|
630
|
-
// Re-confirm lock ownership after drift correction. If another process has
|
|
631
|
-
// taken the lock (TAB_LOCKED), this throws and surfaces as an explicit error
|
|
632
|
-
// rather than silent commands targeting the wrong tab.
|
|
633
|
-
// Pass loc.group so the re-acquire does not strip group membership from the lock file.
|
|
634
|
-
const { acquireLock } = await import('../working-location.js');
|
|
675
|
+
// Refresh context and re-confirm the lock as one binding transaction. A
|
|
676
|
+
// newer attach/release changes the durable pointer and aborts this repair.
|
|
635
677
|
try {
|
|
636
|
-
|
|
678
|
+
withAgentBindingClaim(() => {
|
|
679
|
+
const current = getDurableWorkingLocation();
|
|
680
|
+
const currentBinding = getLocalSessionBindingSnapshot(true);
|
|
681
|
+
if (!current
|
|
682
|
+
|| current.tabId !== loc.tabId
|
|
683
|
+
|| current.setAt !== loc.setAt
|
|
684
|
+
|| current.windowId !== loc.windowId
|
|
685
|
+
|| current.controlSessionId !== loc.controlSessionId
|
|
686
|
+
|| !localSessionBindingAuthorityEquals(currentBinding, initialBinding)) {
|
|
687
|
+
throw new Error('TAB_LOCKED: local binding changed during drift correction');
|
|
688
|
+
}
|
|
689
|
+
const activeLock = getLockedBy(loc.tabId);
|
|
690
|
+
if (activeLock?.reservationId) {
|
|
691
|
+
throw new Error('TAB_LOCKED: tab release or attach transaction is in progress');
|
|
692
|
+
}
|
|
693
|
+
setWorkingLocation({
|
|
694
|
+
tabId: loc.tabId,
|
|
695
|
+
...(typeof loc.windowId === 'number' ? { windowId: loc.windowId } : {}),
|
|
696
|
+
...(loc.group != null ? { group: loc.group } : {}),
|
|
697
|
+
...(loc.controlSessionId ? { controlSessionId: loc.controlSessionId } : {}),
|
|
698
|
+
}, true);
|
|
699
|
+
setLocalSessionContext(workingTabId, reboundAgentSessionId, loc.windowId, true);
|
|
700
|
+
});
|
|
637
701
|
}
|
|
638
702
|
catch (error) {
|
|
639
703
|
this.rethrowTabLock(error);
|
|
640
704
|
}
|
|
641
705
|
}
|
|
642
|
-
|
|
706
|
+
resolveTabOwnership(tabId, commitReclaim = true) {
|
|
643
707
|
const lock = getLockedBy(Number(tabId));
|
|
644
|
-
if (!lock)
|
|
645
|
-
|
|
708
|
+
if (!lock) {
|
|
709
|
+
this.resolvedTabOwnership.set(tabId, 'unlocked');
|
|
710
|
+
return 'unlocked';
|
|
711
|
+
}
|
|
646
712
|
const currentAgentId = resolveAgentId();
|
|
647
713
|
const ownedByAgent = !!lock.agentId && lock.agentId === currentAgentId;
|
|
648
714
|
const ownedByPid = !lock.agentId && lock.pid === process.pid;
|
|
649
|
-
if (ownedByAgent || ownedByPid)
|
|
650
|
-
|
|
715
|
+
if (ownedByAgent || ownedByPid) {
|
|
716
|
+
this.resolvedTabOwnership.set(tabId, 'owned');
|
|
717
|
+
return 'owned';
|
|
718
|
+
}
|
|
651
719
|
const continuity = assessLocalContinuity(getLocalSessionContext(), getWorkingLocation());
|
|
652
720
|
if (continuity.state === 'stale_reclaimable' && continuity.effectiveTabId === tabId) {
|
|
721
|
+
if (!commitReclaim)
|
|
722
|
+
return 'reclaimed';
|
|
653
723
|
try {
|
|
654
|
-
|
|
724
|
+
withAgentBindingClaim(() => {
|
|
725
|
+
const current = getDurableWorkingLocation() ?? getWorkingLocation();
|
|
726
|
+
setWorkingLocation({
|
|
727
|
+
tabId: Number(tabId),
|
|
728
|
+
...(typeof current?.windowId === 'number' ? { windowId: current.windowId } : {}),
|
|
729
|
+
...(current?.group != null ? { group: current.group } : {}),
|
|
730
|
+
...(current?.controlSessionId ? { controlSessionId: current.controlSessionId } : {}),
|
|
731
|
+
}, true);
|
|
732
|
+
});
|
|
655
733
|
}
|
|
656
734
|
catch (error) {
|
|
657
735
|
this.rethrowTabLock(error);
|
|
658
736
|
}
|
|
659
|
-
|
|
737
|
+
// Reclaim changes only the CLI lock. The native host still associates the
|
|
738
|
+
// tab with the previous session, so forwarding our identity would be
|
|
739
|
+
// rejected there. Host re-registration is a separate recovery feature.
|
|
740
|
+
this.resolvedTabOwnership.set(tabId, 'reclaimed');
|
|
741
|
+
return 'reclaimed';
|
|
660
742
|
}
|
|
661
743
|
const owner = lock.agentId
|
|
662
744
|
? `agent ${lock.agentId}`
|
|
663
745
|
: `PID ${lock.ownerShellPid ?? lock.pid}`;
|
|
664
746
|
throw new ApiError(`Tab ${tabId} is owned by another local session (${owner})`, undefined, getNativeCodeHint('TAB_OWNED_BY_OTHER_SESSION'), 'TAB_OWNED_BY_OTHER_SESSION', false);
|
|
665
747
|
}
|
|
748
|
+
/**
|
|
749
|
+
* Identity is forwarded for normal ambient commands. A --tab command only
|
|
750
|
+
* forwards it after target resolution proved the local lock is self-owned.
|
|
751
|
+
* Missing resolution deliberately remains anonymous.
|
|
752
|
+
*/
|
|
753
|
+
getRequestSessionId() {
|
|
754
|
+
if (!this.tabOverride)
|
|
755
|
+
return this.getAgentSessionId();
|
|
756
|
+
const localContext = getLocalSessionContext();
|
|
757
|
+
return this.resolvedTabOwnership.get(this.tabOverride) === 'owned'
|
|
758
|
+
&& localContext?.tabId === this.tabOverride
|
|
759
|
+
? this.getAgentSessionId()
|
|
760
|
+
: undefined;
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* A reclaimed lock is only local state. Do not issue a request the host is
|
|
764
|
+
* known to reject until a dedicated re-registration recovery flow exists.
|
|
765
|
+
*/
|
|
766
|
+
throwReclaimedTabRecoveryRequired(tabId) {
|
|
767
|
+
throw new ApiError(`Tab ${tabId} was reclaimed from a stale local lock, but the native host still owns it through the previous session`, undefined, 'Local stale-lock reclaim does not recover native-host ownership. Ask the prior session to release the tab or attach a different tab. Host re-registration recovery is not available yet.', 'TAB_OWNED_BY_OTHER_SESSION', false);
|
|
768
|
+
}
|
|
769
|
+
/** Resolve a direct session request that bypasses getActiveTabId(). */
|
|
770
|
+
resolveTabOverrideForRequest(path) {
|
|
771
|
+
if (!this.tabOverride || this.resolvedTabOwnership.has(this.tabOverride))
|
|
772
|
+
return;
|
|
773
|
+
const match = path.match(/^\/sessions\/(\d+)(?:\/|$)/);
|
|
774
|
+
if (!match || match[1] !== this.tabOverride)
|
|
775
|
+
return;
|
|
776
|
+
const ownership = this.resolveTabOwnership(this.tabOverride, false);
|
|
777
|
+
if (ownership === 'reclaimed')
|
|
778
|
+
this.throwReclaimedTabRecoveryRequired(this.tabOverride);
|
|
779
|
+
}
|
|
666
780
|
/**
|
|
667
781
|
* Returns the active tab ID, validating it still exists against the native
|
|
668
782
|
* host's /tabs list. Results are cached within this adapter instance's lifetime
|
|
@@ -673,7 +787,9 @@ export class LocalAdapter {
|
|
|
673
787
|
async getActiveTabId() {
|
|
674
788
|
// --tab override: bypass working-location.json entirely
|
|
675
789
|
if (this.tabOverride) {
|
|
676
|
-
this.
|
|
790
|
+
const ownership = this.resolveTabOwnership(this.tabOverride, false);
|
|
791
|
+
if (ownership === 'reclaimed')
|
|
792
|
+
this.throwReclaimedTabRecoveryRequired(this.tabOverride);
|
|
677
793
|
return this.tabOverride;
|
|
678
794
|
}
|
|
679
795
|
await this.ensureWorkingLocationTab();
|
|
@@ -681,7 +797,7 @@ export class LocalAdapter {
|
|
|
681
797
|
if (!tabId) {
|
|
682
798
|
throw localCommandRequiresTabError();
|
|
683
799
|
}
|
|
684
|
-
this.
|
|
800
|
+
this.resolveTabOwnership(tabId);
|
|
685
801
|
const lastValidated = this.validatedTabCache.get(tabId);
|
|
686
802
|
if (lastValidated && Date.now() - lastValidated < this.TAB_CACHE_TTL_MS) {
|
|
687
803
|
return tabId;
|
|
@@ -698,6 +814,13 @@ export class LocalAdapter {
|
|
|
698
814
|
};
|
|
699
815
|
const localContext = getLocalSessionContext();
|
|
700
816
|
const workingLocation = getWorkingLocation();
|
|
817
|
+
let expectedBinding;
|
|
818
|
+
try {
|
|
819
|
+
expectedBinding = getLocalSessionBindingSnapshot();
|
|
820
|
+
}
|
|
821
|
+
catch (error) {
|
|
822
|
+
this.rethrowTabLock(error);
|
|
823
|
+
}
|
|
701
824
|
const expectedTabId = Number(tabId);
|
|
702
825
|
const expectedWindowId = localContext?.windowId;
|
|
703
826
|
const updatedAtMs = typeof localContext?.updatedAt === 'string'
|
|
@@ -724,8 +847,12 @@ export class LocalAdapter {
|
|
|
724
847
|
if (tabs.length > 0) {
|
|
725
848
|
let healedWindowMismatch = false;
|
|
726
849
|
if (!found) {
|
|
727
|
-
|
|
728
|
-
|
|
850
|
+
if (releaseWorkingLocationForTab(expectedTabId, workingLocation?.group)
|
|
851
|
+
|| canClearContextAfterTargetedReleaseFailure(expectedTabId)) {
|
|
852
|
+
if (!clearLocalSessionContext()) {
|
|
853
|
+
throw new ApiError(`Tab ${tabId} cleanup is contended by another binding transaction`, undefined, 'Retry the command after the current attach or release finishes.', 'TAB_LOCKED', true);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
729
856
|
throw new SessionError(`Tab ${tabId} is no longer open`, 'Run: thinkrun tabs\nThen: thinkrun attach <tabId>');
|
|
730
857
|
}
|
|
731
858
|
if (fingerprintMismatch()) {
|
|
@@ -734,36 +861,74 @@ export class LocalAdapter {
|
|
|
734
861
|
// Extension reconnects can legitimately re-home the same owned tab into a
|
|
735
862
|
// different window record. Heal that drift only when the /tabs entry still
|
|
736
863
|
// proves both the expected tab id and the same runtime owner session.
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
864
|
+
try {
|
|
865
|
+
withAgentBindingClaim(() => {
|
|
866
|
+
const durable = getDurableWorkingLocation();
|
|
867
|
+
const currentBinding = getLocalSessionBindingSnapshot(true);
|
|
868
|
+
if (!durable
|
|
869
|
+
|| durable.tabId !== workingLocation?.tabId
|
|
870
|
+
|| durable.setAt !== workingLocation.setAt
|
|
871
|
+
|| currentBinding.privateTabId !== localContext?.tabId
|
|
872
|
+
|| (currentBinding.privateAgentSessionId
|
|
873
|
+
&& currentBinding.privateAgentSessionId !== healedAgentSessionId)) {
|
|
874
|
+
throw new Error('TAB_LOCKED: local binding changed before window repair');
|
|
875
|
+
}
|
|
876
|
+
setWorkingLocation({
|
|
877
|
+
tabId: expectedTabId,
|
|
878
|
+
windowId: found.windowId,
|
|
879
|
+
...(workingLocation?.group ? { group: workingLocation.group } : {}),
|
|
880
|
+
...(localContext?.controlSessionId ? { controlSessionId: localContext.controlSessionId } : {}),
|
|
881
|
+
}, true);
|
|
882
|
+
setLocalSessionContext(String(expectedTabId), healedAgentSessionId, found.windowId, true);
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
catch {
|
|
886
|
+
throw new SessionError(`Tab ${tabId} binding changed during window repair`, 'Retry the command after the current attach or release finishes.');
|
|
887
|
+
}
|
|
744
888
|
healedWindowMismatch = true;
|
|
745
889
|
}
|
|
746
890
|
else {
|
|
747
|
-
|
|
748
|
-
|
|
891
|
+
if (releaseWorkingLocationForTab(expectedTabId, workingLocation?.group)
|
|
892
|
+
|| canClearContextAfterTargetedReleaseFailure(expectedTabId)) {
|
|
893
|
+
if (!clearLocalSessionContext()) {
|
|
894
|
+
throw new ApiError(`Tab ${tabId} cleanup is contended by another binding transaction`, undefined, 'Retry the command after the current attach or release finishes.', 'TAB_LOCKED', true);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
749
897
|
throw new SessionError(`Tab ${tabId} no longer matches the persisted local binding`, `Persisted continuity expected tab ${tabId} in window ${expectedWindowId}, but the live browser now reports window ${found.windowId}. This usually means the original tab was closed and the browser reused the tab ID. Run: thinkrun tabs\nThen: thinkrun attach <tabId>`);
|
|
750
898
|
}
|
|
751
899
|
}
|
|
752
900
|
if (!healedWindowMismatch && typeof found.windowId === 'number' && localContext?.windowId !== found.windowId) {
|
|
753
|
-
|
|
901
|
+
try {
|
|
902
|
+
withAgentBindingClaim(() => {
|
|
903
|
+
const durable = getDurableWorkingLocation();
|
|
904
|
+
const currentBinding = getLocalSessionBindingSnapshot(true);
|
|
905
|
+
if (!workingLocation
|
|
906
|
+
|| !durable
|
|
907
|
+
|| durable.tabId !== workingLocation.tabId
|
|
908
|
+
|| durable.setAt !== workingLocation.setAt
|
|
909
|
+
|| !localSessionBindingAuthorityEquals(currentBinding, expectedBinding)) {
|
|
910
|
+
throw new Error('TAB_LOCKED: local binding changed before window metadata refresh');
|
|
911
|
+
}
|
|
912
|
+
setLocalSessionContext(tabId, localContext?.agentSessionId, found.windowId, true);
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
catch (error) {
|
|
916
|
+
this.rethrowTabLock(error);
|
|
917
|
+
}
|
|
754
918
|
}
|
|
755
919
|
this.validatedTabCache.set(tabId, Date.now());
|
|
756
920
|
}
|
|
757
921
|
// If validation fetch returns non-200, skip caching and let command fail naturally
|
|
758
922
|
}
|
|
759
923
|
catch (error) {
|
|
760
|
-
if (error instanceof SessionError)
|
|
924
|
+
if (error instanceof SessionError || error instanceof ApiError)
|
|
761
925
|
throw error;
|
|
762
926
|
// Swallow network errors during validation — the actual command will give a better error
|
|
763
927
|
}
|
|
764
928
|
return tabId;
|
|
765
929
|
}
|
|
766
930
|
async request(path, options = {}) {
|
|
931
|
+
this.resolveTabOverrideForRequest(path);
|
|
767
932
|
const httpMs = options.timeoutMs ?? this.defaultTimeout;
|
|
768
933
|
const method = (options.method ?? 'GET').toUpperCase();
|
|
769
934
|
const requestId = `local-${randomUUID().slice(0, 8)}`;
|
|
@@ -777,9 +942,7 @@ export class LocalAdapter {
|
|
|
777
942
|
'Content-Type': 'application/json',
|
|
778
943
|
'x-thinkrun-request-id': requestId,
|
|
779
944
|
};
|
|
780
|
-
|
|
781
|
-
// belongs to whoever attached, not to us. The tabId in the URL path is sufficient.
|
|
782
|
-
const sessionId = this.tabOverride ? undefined : this.getAgentSessionId();
|
|
945
|
+
const sessionId = this.getRequestSessionId();
|
|
783
946
|
if (sessionId) {
|
|
784
947
|
headers['x-session-id'] = sessionId;
|
|
785
948
|
}
|
|
@@ -1193,11 +1356,16 @@ export class LocalAdapter {
|
|
|
1193
1356
|
const tabId = await this.getActiveTabId();
|
|
1194
1357
|
const auditActionId = this.createAuditActionId('type');
|
|
1195
1358
|
const pre = await this.tryCaptureFP(selector);
|
|
1359
|
+
const delayMs = typeof options.delay === 'number' && Number.isFinite(options.delay) && options.delay > 0
|
|
1360
|
+
? options.delay
|
|
1361
|
+
: 0;
|
|
1362
|
+
const timeoutMs = Math.min(600_000, Math.max(this.defaultTimeout, Array.from(text).length * delayMs + 5_000));
|
|
1196
1363
|
let response;
|
|
1197
1364
|
try {
|
|
1198
1365
|
response = await this.request(`/sessions/${tabId}/type`, {
|
|
1199
1366
|
method: 'POST',
|
|
1200
|
-
body: JSON.stringify({ selector, text, delay: options.delay }),
|
|
1367
|
+
body: JSON.stringify({ selector, text, delay: options.delay, clear: options.clear, timeout: timeoutMs }),
|
|
1368
|
+
timeoutMs: timeoutMs + 1_000,
|
|
1201
1369
|
});
|
|
1202
1370
|
}
|
|
1203
1371
|
catch (err) {
|
|
@@ -1230,7 +1398,15 @@ export class LocalAdapter {
|
|
|
1230
1398
|
this.syncAction(tabId, 'type', syncDetails);
|
|
1231
1399
|
this.triggerAuditScreenshot(tabId, 'type', this.buildAuditCaptureDetails('type', auditActionId, syncDetails));
|
|
1232
1400
|
}
|
|
1233
|
-
|
|
1401
|
+
const editableData = response.data && typeof response.data === 'object'
|
|
1402
|
+
? {
|
|
1403
|
+
controlType: typeof response.data.controlType === 'string'
|
|
1404
|
+
? response.data.controlType
|
|
1405
|
+
: 'unknown',
|
|
1406
|
+
changed: response.data.changed === true,
|
|
1407
|
+
}
|
|
1408
|
+
: undefined;
|
|
1409
|
+
return { success: response.success, ...(editableData ? { data: editableData } : {}) };
|
|
1234
1410
|
}
|
|
1235
1411
|
async fill(selector, value) {
|
|
1236
1412
|
const tabId = await this.getActiveTabId();
|
|
@@ -1273,7 +1449,15 @@ export class LocalAdapter {
|
|
|
1273
1449
|
this.syncAction(tabId, 'fill', syncDetails);
|
|
1274
1450
|
this.triggerAuditScreenshot(tabId, 'fill', this.buildAuditCaptureDetails('fill', auditActionId, syncDetails));
|
|
1275
1451
|
}
|
|
1276
|
-
|
|
1452
|
+
const editableData = response.data && typeof response.data === 'object'
|
|
1453
|
+
? {
|
|
1454
|
+
controlType: typeof response.data.controlType === 'string'
|
|
1455
|
+
? response.data.controlType
|
|
1456
|
+
: 'unknown',
|
|
1457
|
+
changed: response.data.changed === true,
|
|
1458
|
+
}
|
|
1459
|
+
: undefined;
|
|
1460
|
+
return { success: response.success, ...(editableData ? { data: editableData } : {}) };
|
|
1277
1461
|
}
|
|
1278
1462
|
async press(key) {
|
|
1279
1463
|
const tabId = await this.getActiveTabId();
|