@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
78 lines • 3.17 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 { conditionBasis, keepSent, stale } from "../../cache/context.js";
import {} from "../../cache/types.js";
import * as kp from "../../utils/key_prefix.js";
/** The refusal for a one-path op whose condition lost. Mirrors Python's `refused`. */
export function refused(path, err, cond) {
return stale(path, { version: conditionBasis(cond) });
}
/**
* Keep every lost key's version, then throw the walk's later error. Mirrors
* Python's `keep_all_lost`.
*/
export async function keepAllLost(root, keyPrefix, err) {
await keepLost(root, keyPrefix, err, null);
throw err.error;
}
/**
* Keep what a walk's refusal measured, and name the key it reports: every
* lost key but the named one keeps its version; a later error that stopped
* the walk is thrown instead, with every version kept. Mirrors Python's
* `keep_walk`.
*/
export async function keepWalk(root, keyPrefix, err) {
if (err.error !== null)
await keepAllLost(root, keyPrefix, err);
const key = err.keys[0] ?? '';
await keepLost(root, keyPrefix, err, key);
return key;
}
/**
* Keep the version of each key a walk lost on, except `skip`, which the
* refusal itself names and keeps. A key found gone keeps what the session
* last saw of it. Mirrors Python's `keep_lost`.
*/
export async function keepLost(root, keyPrefix, err, skip) {
for (const [key, version] of err.versions) {
const kept = typeof version === 'string' ? version : null;
if (key !== skip)
await keepSent(kp.keyPath(root, keyPrefix, key), kept);
}
}
/**
* The `*If` hook a conditional op calls. Every `write: conditional` object
* store is on the S3 driver, which carries them all; Box and Dropbox condition
* their own ops and never reach here.
*/
export function requireHook(hook) {
if (hook === undefined)
throw new Error('conditional op on a driver without its hook');
return hook;
}
/**
* Whether a prefix holds a child, as `readdir` would list one. The prefix's
* own marker is no child: it proves only that the directory exists, and one
* made again after a removal is a later `mkdir`, not a key the removal
* missed. The listing stops at the first child. Mirrors Python's
* `holds_child`.
*/
export async function holdsChild(driver, conn, pfx) {
for await (const child of driver.listChildren(conn, pfx)) {
if (child.kind !== 'marker')
return true;
}
return false;
}
//# sourceMappingURL=driver.js.map