@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
61 lines • 2.77 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { currentCaller, liveCallers, runWithCaller } from "../../context/session_context.js";
let sequence = 0;
/**
* The next point in the one order commands and listing writes share.
*
* A sequence rather than a clock: two events in the same clock tick still
* come out ordered, so "written after this command started" is exact.
*/
export function tick() {
sequence += 1;
return sequence;
}
/**
* When the running command started, or null outside any command.
*
* On an isolating runtime that is the current task's stamp. On the
* fallback storage another command's frame can sit on top, so this
* answers the latest live stamp: a later stamp trusts fewer listings,
* which fails toward a re-list rather than toward a stale one.
*/
export function commandStarted() {
const stamps = liveCallers().flatMap((caller) => caller.started === null ? [] : [caller.started]);
return stamps.length === 0 ? null : Math.max(...stamps);
}
/**
* The running command's own stamp, for a check that it fetched a listing.
*
* On an isolating runtime that is `commandStarted`. On the fallback storage
* the latest live stamp may be another command's, so with more than one
* command live this answers null: no listing is proven the caller's own. A
* replayed scope (a command's output drained later) repeats its own stamp,
* so stamps are counted once each. Mirrors Python's `sole_command_started`.
*/
export function soleCommandStarted() {
const stamps = new Set(liveCallers().flatMap((caller) => (caller.started === null ? [] : [caller.started])));
return stamps.size === 1 ? (stamps.values().next().value ?? null) : null;
}
/**
* Mark one command's run, so a fresh listing it writes can be trusted.
*
* Entered before the command's words expand, so its own globs count. A
* nested command (a `$(...)`, a function body) gets its own later stamp,
* and the outer one comes back when it ends.
*/
export function runInCommandScope(fn) {
return runWithCaller({ ...currentCaller(), started: tick() }, fn);
}
//# sourceMappingURL=scope.js.map