Files
Codeman/src/types/common.ts
T
Codeman maintainer 4ea781c80f feat(file-viewer): edit mode for text files (edit + save in the viewer)
Closes #212. The file-preview overlay can now edit workspace text files in
place, phone-first: agent writes a file, you review it in the viewer, tweak
two lines, save, tell the agent to continue.

Backend (file-routes.ts, policy in src/config/file-editing.ts):
- GET file-content?edit=1: read-for-edit that never truncates (a truncated
  buffer must never become an edit buffer), 512KB cap (413 over it), and
  returns the sha256 hash + detected EOL the client echoes back on save.
- PUT /api/sessions/:id/file-content: edit-in-place only, with no O_CREAT
  anywhere in the handler. Confinement matches the read path (realpath +
  workspace boundary + ownership via findSessionOrFail), plus sensitive-path
  and attachment-guard blocklists, a .git subtree deny, and an extension
  allowlist (svg and env deliberately excluded). Optimistic concurrency via
  baseHash: mismatch is a 409 unless force. Writes are wx-temp + fchmod +
  fsync + rename, closing the validate-then-write TOCTOU window.
- Corruption guards: NUL sniff + UTF-8 round-trip compare (refuses binary
  and latin-1), and server-side EOL re-application so a textarea's LF
  normalization cannot rewrite every line of a CRLF file.
- Plain reads gain an additive editable flag the UI keys the button off.

Frontend (panels-ui.js + overlay markup/styles):
- Edit button on editable text previews; textarea editor with Save/Cancel,
  dirty indicator, discard-confirm on cancel/close, and a conflict dialog
  that offers overwrite (force) when the file changed on disk mid-edit.
- Phone: full-bleed window sized by --app-height so the editor and Save bar
  track the OS keyboard; 16px editor font (iOS zoom guard); no autofocus.
- zh-CN strings for the new chrome.

Tests: pure policy unit tests plus a route suite that deliberately does NOT
mock node:fs. It runs against a real temp workspace so symlink escapes,
write-through of in-workspace symlinks, mode preservation, CRLF round-trip,
409/force, and the no-create property are exercised for real. Also verified
end to end on an isolated beta instance: 39-check curl matrix, Playwright
desktop flow (real clicks and typing, bytes asserted on disk, live conflict
with an external rewrite), and a 393px phone profile.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-05 08:44:47 +02:00

134 lines
3.9 KiB
TypeScript

/**
* @fileoverview Common/shared type definitions.
*
* Base types used across multiple domains. No dependencies on other domain modules.
*
* Key exports:
* - Disposable — interface for objects requiring explicit cleanup (timers, watchers)
* - BufferConfig — size-limited storage config (terminal: 2MB, text: 1MB)
* - CleanupRegistration / CleanupResourceType — entries for the centralized CleanupManager
* - NiceConfig / DEFAULT_NICE_CONFIG — process priority settings for `nice`/`ionice`
* - ProcessStats — memory/CPU/child-count snapshot for resource monitoring
* - FilesystemBrowseData — bounded path-picker directory listing returned to the web UI
*/
/**
* Interface for objects that hold resources requiring explicit cleanup.
* Implementing classes should release timers, watchers, and other resources in dispose().
*/
export interface Disposable {
/** Release all held resources. Safe to call multiple times. */
dispose(): void;
/** Whether this object has been disposed */
readonly isDisposed: boolean;
}
/**
* Configuration for buffer accumulator instances.
* Used for terminal buffers, text output, and other size-limited string storage.
*/
export interface BufferConfig {
/** Maximum buffer size in bytes before trimming */
maxSize: number;
/** Size to trim to when maxSize is exceeded */
trimSize: number;
/** Optional callback invoked when buffer is trimmed */
onTrim?: (trimmedBytes: number) => void;
}
/**
* Resource types that can be registered for cleanup.
*/
/**
* Configuration for process priority using `nice`.
* Lower priority reduces CPU contention with other processes.
*/
export interface NiceConfig {
/** Whether nice priority is enabled */
enabled: boolean;
/** Nice value (-20 to 19, default: 10 = lower priority) */
niceValue: number;
}
export const DEFAULT_NICE_CONFIG: NiceConfig = {
enabled: false,
niceValue: 10,
};
/**
* Process resource statistics
*/
export interface ProcessStats {
/** Memory usage in megabytes */
memoryMB: number;
/** CPU usage percentage */
cpuPercent: number;
/** Number of child processes */
childCount: number;
/** Timestamp of stats collection */
updatedAt: number;
}
/** A selectable entry returned by the filesystem path-picker API. */
export type FilesystemPreviewKind = 'image' | 'text' | 'document';
export interface FilesystemBrowseEntry {
name: string;
path: string;
type: 'file' | 'directory';
size?: number;
symlink?: boolean;
previewKind?: FilesystemPreviewKind;
}
/** A named root the path picker may browse without escaping its allowlist. */
export interface FilesystemBrowseRoot {
label: string;
path: string;
}
/** Response payload for `GET /api/filesystem/browse`. */
export interface FilesystemBrowseData {
path: string;
parent: string | null;
root: string;
roots: FilesystemBrowseRoot[];
entries: FilesystemBrowseEntry[];
truncated: boolean;
}
/** Response payload for `PUT /api/sessions/:id/file-content` (File Viewer edit mode). */
export interface FileWriteData {
/** Workspace-relative path as submitted */
path: string;
/** Size of the written content in bytes */
size: number;
/** mtime of the file after the write */
mtimeMs: number;
/** sha256 hex of the written bytes — the client's next baseHash */
hash: string;
/** Line count of the written content */
totalLines: number;
/** Line-ending style that was applied */
eol: 'lf' | 'crlf';
}
export type CleanupResourceType = 'timer' | 'interval' | 'watcher' | 'listener' | 'stream';
/**
* Registration entry for a cleanup resource.
* Used by CleanupManager to track and dispose resources.
*/
export interface CleanupRegistration {
/** Unique identifier for this registration */
id: string;
/** Type of resource */
type: CleanupResourceType;
/** Human-readable description for debugging */
description: string;
/** Cleanup function to call on dispose */
cleanup: () => void;
/** Timestamp when registered */
registeredAt: number;
}