Files
Strata/app/host/server/user-rules.mjs
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

68 lines
3.4 KiB
JavaScript

/**
* The person's own rules for every answer (#5084): one plain-text file, `<data>/agent/home/AGENTS.md`.
*
* That is the user-global AGENTS.md the Harness already reads in agent mode (its agent-instructions plugin looks in DSH_HOME, which the
* host sets to `<data>/agent/home`), so the coding assistant picks the rules up by itself. The plain chat paths (the local model and an
* API connection) get the same text through `promptBlock()`. Empty or missing rules add nothing at all, so the system prompt stays
* byte-identical to the one without this feature and the engine's prompt cache is unaffected.
*
* Writes carry the revision the editor read: a save over a file another window changed since is refused (409), never merged silently.
*/
import { createHash } from 'node:crypto';
import { mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
/** The Harness plugin's own limit (preset.mjs: agent-instructions maxBytes 65536); the editor keeps to it so agent mode reads all of it. */
export const RULES_MAX_BYTES = 65536;
export class RulesError extends Error {
constructor(message, status, code) { super(message); this.status = status; this.code = code; }
}
const revisionOf = text => createHash('sha256').update(text, 'utf8').digest('hex').slice(0, 16);
export function rulesFile(dataDir) { return join(dataDir, 'agent', 'home', 'AGENTS.md'); }
export function createUserRules({ file }) {
let cache = null; // { mtimeMs, size, text }
function current() {
let st;
try { st = statSync(file); } catch { cache = null; return ''; }
if (cache && cache.mtimeMs === st.mtimeMs && cache.size === st.size) return cache.text;
let text = '';
try { text = readFileSync(file, 'utf8'); } catch { text = ''; }
if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
cache = { mtimeMs: st.mtimeMs, size: st.size, text };
return text;
}
/** What the editor shows, and the revision a save must name. */
function read() {
const text = current();
return { text, revision: revisionOf(text), bytes: Buffer.byteLength(text, 'utf8'), maxBytes: RULES_MAX_BYTES, path: file };
}
/** Save the editor's text. `revision` is the one `read()` gave; a different file on disk now means another window saved first. */
function write(text, revision) {
if (typeof text !== 'string') throw new RulesError('The rules must be text.', 400, 'bad-text');
const normalized = text.replace(/\r\n?/g, '\n');
if (Buffer.byteLength(normalized, 'utf8') > RULES_MAX_BYTES) throw new RulesError('These rules are too long. Make them shorter than 64 KB, then save again.', 413, 'too-long');
if (revision !== revisionOf(current())) throw new RulesError('The rules were changed in another window. Close this and open them again to see the newest version.', 409, 'changed');
mkdirSync(dirname(file), { recursive: true });
const tmp = `${file}.${process.pid}.tmp`;
writeFileSync(tmp, normalized, 'utf8');
renameSync(tmp, file);
cache = null;
return read();
}
/** The block the plain chat paths add after Strata's own prompt: '' when there are no rules, so nothing changes then. */
function promptBlock() {
const text = current().trim();
return text ? `The person you are helping set these rules for every answer:\n${text}` : '';
}
return { read, write, promptBlock, file };
}