import { DurableObject } from "cloudflare:workers";
import { isSafeTreePath } from "../git/tree-path";
import { containerStillAuthorized } from "./container-authz";
import { containerLimits } from "./container-config";
import {
abandonContainerSession,
closeContainerSession,
ContainerCapError,
openContainerSession,
} from "./container-meter";
import { QuotaExceededError } from "../metering/quota";
import type { Reservation } from "../metering/quota";
const MINUTE_MS = 60 * 1000;
const SECOND_MS = 1000;
const CHECK_MS = MINUTE_MS;
const SETTLE_RETRY_MS = 30 * SECOND_MS;
const READY_ATTEMPTS = 60;
const READY_DELAY_MS = 500;
const ACTIVITY_WRITE_MS = 30 * 1000;
const MAX_SYNC_FILES = 300;
const DEFAULT_COLS = 120;
const DEFAULT_ROWS = 32;
const MAX_TERMINALS = 8;
const MAX_CONTROL_FRAME_BYTES = 1024;
const WORKSPACE = "/workspace";
const CONTAINER_HOME = "/home/dev";
/**
* Writes (or deletes) one file under the checkout, refusing when any existing path component is a symlink:
* the room is shared per branch but containers are per user, so a committed `link -> $HOME` must not let a
* co-editor's room edit land in this user's home directory.
*/
const SYNC_SCRIPT = `set -e
root="$1"; rel="$2"; mode="$3"; dir="$root"
set -f; oldIFS=$IFS; IFS=/
for part in $rel; do
if [ -L "$dir/$part" ]; then echo "symlink in path" >&2; exit 3; fi
dir="$dir/$part"
done