@fatelabs/opencode-estelle 0.0.0-stage → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +6 -0
- package/README.md +46 -2
- package/adapters/claude.mjs +17 -0
- package/adapters/codex.mjs +85 -0
- package/adapters/cursor.mjs +66 -0
- package/adapters/dsh-host.mjs +181 -0
- package/adapters/dsh-plugin.mjs +11 -0
- package/adapters/dsh.mjs +54 -0
- package/adapters/grok.mjs +63 -0
- package/adapters/host-bridge.mjs +85 -0
- package/adapters/kimi.mjs +135 -0
- package/adapters/opencode-mirror.mjs +103 -0
- package/adapters/opencode-plugin.mjs +143 -0
- package/adapters/opencode.mjs +15 -0
- package/adapters/pi-extension.mjs +107 -0
- package/adapters/pi.mjs +13 -0
- package/adapters/plugin-envelope.mjs +56 -0
- package/agents/estelle-grounder.md +34 -0
- package/agents/estelle-historian.md +28 -0
- package/agents/estelle-repair.md +29 -0
- package/agents/estelle-reviewer.md +32 -0
- package/commands/cloud.md +28 -0
- package/commands/code.md +6 -0
- package/commands/handoff.md +25 -0
- package/commands/monitor.md +6 -0
- package/commands/resume.md +6 -0
- package/commands/review.md +6 -0
- package/commands/rooms.md +7 -0
- package/instructions.md +19 -0
- package/package.json +34 -4
- package/runtime/auth.mjs +27 -0
- package/runtime/checkpoint.mjs +208 -0
- package/runtime/claude-setup.mjs +130 -0
- package/runtime/cli.mjs +112 -0
- package/runtime/codex-trust.mjs +68 -0
- package/runtime/credential.mjs +341 -0
- package/runtime/dsh-session.mjs +91 -0
- package/runtime/dsh-telemetry.mjs +31 -0
- package/runtime/hooks.mjs +234 -0
- package/runtime/identity.mjs +20 -0
- package/runtime/io.mjs +155 -0
- package/runtime/key-check.mjs +51 -0
- package/runtime/kimi-history.mjs +163 -0
- package/runtime/mailbox.mjs +46 -0
- package/runtime/migration.mjs +118 -0
- package/runtime/move.mjs +443 -0
- package/runtime/native-export.mjs +204 -0
- package/runtime/native-index.mjs +77 -0
- package/runtime/native-tree.mjs +117 -0
- package/runtime/queued-context.mjs +152 -0
- package/runtime/redact.mjs +74 -0
- package/runtime/reindex.mjs +259 -0
- package/runtime/setup.mjs +174 -0
- package/runtime/stream.mjs +755 -0
- package/runtime/version.mjs +2 -0
- package/runtime/write-warning.mjs +67 -0
- package/skills/abstain-right/SKILL.md +19 -0
- package/skills/api-call-ground/SKILL.md +19 -0
- package/skills/bug-hunt/SKILL.md +20 -0
- package/skills/chain-of-verification/SKILL.md +19 -0
- package/skills/change-impact/SKILL.md +19 -0
- package/skills/coding-workflow/SKILL.md +20 -0
- package/skills/commit-guard/SKILL.md +20 -0
- package/skills/context-checkpoint/SKILL.md +19 -0
- package/skills/continuity-brief/SKILL.md +19 -0
- package/skills/dead-code-sweep/SKILL.md +19 -0
- package/skills/deep-search/SKILL.md +19 -0
- package/skills/diff-review/SKILL.md +19 -0
- package/skills/estelle/SKILL.md +119 -0
- package/skills/estelle-loop/SKILL.md +19 -0
- package/skills/estelle-monitor-install/SKILL.md +20 -0
- package/skills/evidence-traceability-audit/SKILL.md +19 -0
- package/skills/grounded-generate/SKILL.md +19 -0
- package/skills/incident-rca/SKILL.md +20 -0
- package/skills/mutation-test/SKILL.md +19 -0
- package/skills/program-repair/SKILL.md +20 -0
- package/skills/prompt-injection-defense/SKILL.md +19 -0
- package/skills/property-based-test/SKILL.md +19 -0
- package/skills/refactor-rfc/SKILL.md +19 -0
- package/skills/root-cause-loop/SKILL.md +19 -0
- package/skills/test-first/SKILL.md +19 -0
- package/skills/threat-model-stride/SKILL.md +19 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { nativeGit, indexState, indexSpelling } from './native-index.mjs';
|
|
2
|
+
import { open, realpath, lstat } from 'node:fs/promises';
|
|
3
|
+
import { resolve, relative, isAbsolute, sep, basename } from 'node:path';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import { constants } from 'node:fs';
|
|
7
|
+
import { git, repository, apiClient, errorCode } from './io.mjs';
|
|
8
|
+
import { redact, isSensitiveField } from './redact.mjs';
|
|
9
|
+
|
|
10
|
+
const MAX_BYTES = 512 * 1024;
|
|
11
|
+
const MAX_FILES = 40;
|
|
12
|
+
const MAX_SCALAR_CHARS = 8192;
|
|
13
|
+
// A long tool result is normal. A long string is scanned in overlapping windows and refused if a credential is found,
|
|
14
|
+
// matching the server's native_session_restore, whose scanner is quadratic on an unbroken run.
|
|
15
|
+
const SCAN_OVERLAP_CHARS = 1024;
|
|
16
|
+
const PRIVATE_TYPES = new Set(['thinking', 'redacted_thinking', 'reasoning', 'agent_reasoning', 'agent_reasoning_raw_content']);
|
|
17
|
+
const PRIVATE_KEYS = new Set(['thinking', 'reasoning', 'reasoning_content', 'encrypted_content']);
|
|
18
|
+
const UUID = /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/;
|
|
19
|
+
const RESERVED = new Set(['.git', '.env', '.claude', '.codex', '.kimi', '.kimi-code', '.estelle', '.estelle-plugins',
|
|
20
|
+
'stress_accounts.json', 'estelle_accounts.json', 'estelle_experience.json']);
|
|
21
|
+
const digest = text => createHash('sha256').update(text).digest('hex');
|
|
22
|
+
const SAFE_CODES = new Set(['native_public_repository', 'native_artifact_bound', 'native_artifact_changed', 'native_artifact_shape',
|
|
23
|
+
'native_selection', 'native_repository', 'native_private_reasoning_unsupported', 'native_credential_field',
|
|
24
|
+
'native_consent_required', 'native_staged_changes_unsupported', 'native_untracked_changes_unsupported',
|
|
25
|
+
'native_tree_transport_unsupported', 'native_untracked_selection_required', 'native_restore_unverified', 'native_index_state_unsupported',
|
|
26
|
+
'native_receipt_mismatch', 'native_scalar_bound', 'native_reserved_path', 'native_source_credential_or_binary', 'native_file_mode_unsupported', 'native_arguments']);
|
|
27
|
+
export const nativeErrorCode = error => SAFE_CODES.has(error?.message) ? error.message : errorCode(error);
|
|
28
|
+
const inside = (root, file) => {
|
|
29
|
+
const rel = relative(root, file);
|
|
30
|
+
return rel && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export async function readNativeBytes(path, limit = MAX_BYTES, expected = null) {
|
|
34
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
35
|
+
try {
|
|
36
|
+
const info = await handle.stat();
|
|
37
|
+
if (expected && (info.ino !== expected.ino || info.dev !== expected.dev)) throw new Error('native_artifact_changed');
|
|
38
|
+
if (!info.isFile() || info.size > limit) throw new Error('native_artifact_bound');
|
|
39
|
+
const bytes = Buffer.alloc(info.size + 1);
|
|
40
|
+
const out = await handle.read(bytes, 0, bytes.length, 0);
|
|
41
|
+
const after = await handle.stat();
|
|
42
|
+
if (out.bytesRead !== info.size || after.size !== info.size || after.mtimeMs !== info.mtimeMs) throw new Error('native_artifact_changed');
|
|
43
|
+
return bytes.subarray(0, out.bytesRead);
|
|
44
|
+
} finally { await handle.close(); }
|
|
45
|
+
}
|
|
46
|
+
async function readExact(path, limit = MAX_BYTES, expected = null) {
|
|
47
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(await readNativeBytes(path, limit, expected));
|
|
48
|
+
}
|
|
49
|
+
export const NATIVE_LIMITS = Object.freeze({ bytes: MAX_BYTES, files: MAX_FILES, blobBytes: 8192 });
|
|
50
|
+
export function checkNativeRelativePath(path) {
|
|
51
|
+
if (typeof path !== 'string' || !path || path.length > 512 || path.split('/').length > 32
|
|
52
|
+
|| path.split('/').some(part => !part || part === '.' || part === '..' || RESERVED.has(part.toLowerCase()) || part.toLowerCase().startsWith('.env.'))
|
|
53
|
+
|| /[\x00-\x1f\\]/.test(path)) throw new Error('native_reserved_path');
|
|
54
|
+
return path;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function nativeUsage(key, value) {
|
|
58
|
+
const counter = name => /^(?:(?:input|output|total|reasoning|cached|cached_input|reasoning_output|cache_write_input|cache_creation_input|cache_read_input)_tokens|max_tokens|token_count)$/.test(name);
|
|
59
|
+
// A count under a `token` name (Claude's ephemeral_5m_input_tokens, whatever comes next) is not a credential.
|
|
60
|
+
if (typeof value === 'number') return Number.isFinite(value) && value >= 0 && (counter(key) || /tokens?$/i.test(key));
|
|
61
|
+
const envelope = /(?:token_usage|tokens_details)$/.test(key);
|
|
62
|
+
return envelope && value && typeof value === 'object' && !Array.isArray(value)
|
|
63
|
+
&& Object.values(value).every(count => typeof count === 'number' && Number.isFinite(count) && count >= 0);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function withoutReasoning(value, depth = 0) {
|
|
67
|
+
if (depth > 16) throw new Error('native_artifact_shape');
|
|
68
|
+
if (Array.isArray(value)) return value.filter(item => !PRIVATE_TYPES.has(item?.type)).map(item => withoutReasoning(item, depth + 1));
|
|
69
|
+
if (!value || typeof value !== 'object') return value;
|
|
70
|
+
return Object.fromEntries(Object.entries(value).filter(([key]) => !PRIVATE_KEYS.has(key))
|
|
71
|
+
.map(([key, item]) => [key, withoutReasoning(item, depth + 1)]));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function scrubString(text) {
|
|
75
|
+
if (text.length <= MAX_SCALAR_CHARS) return redact(text);
|
|
76
|
+
for (let start = 0; start < text.length; start += MAX_SCALAR_CHARS - SCAN_OVERLAP_CHARS) {
|
|
77
|
+
const window = text.slice(start, start + MAX_SCALAR_CHARS);
|
|
78
|
+
if (redact(window) !== window) throw new Error('native_credential_field');
|
|
79
|
+
}
|
|
80
|
+
return text;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function scrubJsonl(text, harness, sessionId) {
|
|
84
|
+
const parsed = text.split('\n').filter(Boolean).map(line => JSON.parse(line));
|
|
85
|
+
if (parsed.length > 4000) throw new Error('native_artifact_bound');
|
|
86
|
+
if (!parsed.length || parsed.some(row => !row || typeof row !== 'object' || Array.isArray(row))) throw new Error('native_artifact_shape');
|
|
87
|
+
// Private reasoning never leaves this machine, and neither host needs an old turn's to continue.
|
|
88
|
+
const rows = parsed.filter(row => !PRIVATE_TYPES.has(row?.payload?.type)).map(row => withoutReasoning(row));
|
|
89
|
+
if (harness === 'claude-code' && rows.some(row => row.sessionId !== sessionId)) throw new Error('native_selection');
|
|
90
|
+
if (harness === 'codex' && (rows[0].type !== 'session_meta' || rows[0].payload?.id !== sessionId)) throw new Error('native_selection');
|
|
91
|
+
let fields = 0;
|
|
92
|
+
const pending = rows.map(value => ({ value, depth: 0 }));
|
|
93
|
+
while (pending.length) {
|
|
94
|
+
const { value, depth } = pending.pop();
|
|
95
|
+
if (!value || typeof value !== 'object' || depth > 16) throw new Error('native_artifact_shape');
|
|
96
|
+
for (const key of Object.keys(value)) {
|
|
97
|
+
if (++fields > 40_000) throw new Error('native_artifact_bound');
|
|
98
|
+
const usage = nativeUsage(key, value[key]);
|
|
99
|
+
if (!usage && (isSensitiveField(key) || /^authorization$/i.test(key))) throw new Error('native_credential_field');
|
|
100
|
+
if (typeof value[key] === 'string') value[key] = scrubString(value[key]);
|
|
101
|
+
else if (value[key] && typeof value[key] === 'object') pending.push({ value: value[key], depth: depth + 1 });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return rows.map(value => JSON.stringify(value)).join('\n') + '\n';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function captureNativeTranscript({ harness, sessionId, transcriptPath, home = homedir() }) {
|
|
108
|
+
if (!['claude-code', 'codex'].includes(harness) || !UUID.test(sessionId || '')) throw new Error('native_selection');
|
|
109
|
+
const root = resolve(home, harness === 'codex' ? '.codex/sessions' : '.claude/projects');
|
|
110
|
+
const source = resolve(transcriptPath);
|
|
111
|
+
const sourceInfo = await lstat(source);
|
|
112
|
+
if (!inside(root, source) || sourceInfo.isSymbolicLink() || !inside(await realpath(root), await realpath(source))) throw new Error('native_reserved_path');
|
|
113
|
+
if (!basename(source).endsWith(`${sessionId}.jsonl`)) throw new Error('native_selection');
|
|
114
|
+
return scrubJsonl(await readExact(source, MAX_BYTES, sourceInfo), harness, sessionId);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function prepareContinuation({ harness, sessionId, transcriptPath, cwd, siloId, home = homedir(), consent, replace }) {
|
|
118
|
+
if (consent !== true || replace !== true) throw new Error('native_consent_required');
|
|
119
|
+
if (!['claude-code', 'codex'].includes(harness) || !UUID.test(sessionId || '') || !siloId) throw new Error('native_selection');
|
|
120
|
+
const repo = repository(cwd);
|
|
121
|
+
if (!repo) throw new Error('native_repository');
|
|
122
|
+
const run = nativeGit(repo.root);
|
|
123
|
+
const indexBefore = indexState(run);
|
|
124
|
+
const canonicalRoot = await realpath(repo.root);
|
|
125
|
+
await indexSpelling(canonicalRoot, indexBefore);
|
|
126
|
+
const baseCommit = run(['rev-parse', 'HEAD']);
|
|
127
|
+
if (!/^[a-f0-9]{40}$/.test(baseCommit)) throw new Error('native_repository');
|
|
128
|
+
// This first transport refuses states it cannot preserve rather than turning staged work into
|
|
129
|
+
// unstaged work or silently dropping deletes. Untracked files require a later explicit picker.
|
|
130
|
+
if (run(['diff', '--cached', '--name-only', '-z'], { raw: true })) throw new Error('native_staged_changes_unsupported');
|
|
131
|
+
if (run(['ls-files', '--others', '--exclude-standard', '-z'], { raw: true })) throw new Error('native_untracked_changes_unsupported');
|
|
132
|
+
const paths = run(['diff', '--name-only', '-z', 'HEAD', '--'], { raw: true }).split('\0').filter(Boolean);
|
|
133
|
+
if (paths.length > MAX_FILES) throw new Error('native_artifact_bound');
|
|
134
|
+
const files = [];
|
|
135
|
+
const observed = [];
|
|
136
|
+
for (const path of paths) {
|
|
137
|
+
checkNativeRelativePath(path);
|
|
138
|
+
const full = resolve(repo.root, path);
|
|
139
|
+
const info = await lstat(full); // Deletion or symlink is a refusal, never an empty file.
|
|
140
|
+
const canonical = await realpath(full);
|
|
141
|
+
if (!info.isFile() || info.isSymbolicLink() || info.mode & 0o111
|
|
142
|
+
|| !inside(await realpath(repo.root), canonical)
|
|
143
|
+
|| canonical !== resolve(await realpath(repo.root), path)) throw new Error('native_reserved_path');
|
|
144
|
+
const baseMode = run(['ls-tree', 'HEAD', '--', path], { raw: true }).split(' ')[0];
|
|
145
|
+
if (baseMode !== '100644') throw new Error('native_file_mode_unsupported');
|
|
146
|
+
const text = await readExact(full, MAX_BYTES, info);
|
|
147
|
+
if (Array.from(text).length > MAX_SCALAR_CHARS) throw new Error('native_scalar_bound');
|
|
148
|
+
if (text.includes('\0') || redact(text) !== text) throw new Error('native_source_credential_or_binary');
|
|
149
|
+
files.push({ path, text, sha256: digest(text) });
|
|
150
|
+
observed.push({ full, info });
|
|
151
|
+
}
|
|
152
|
+
const transcript = await captureNativeTranscript({ harness, sessionId, transcriptPath, home });
|
|
153
|
+
if (run(['diff', '--cached', '--name-only', '-z'], { raw: true }) || run(['ls-files', '--others', '--exclude-standard', '-z'], { raw: true })) throw new Error('native_artifact_changed');
|
|
154
|
+
if (run(['rev-parse', 'HEAD']) !== baseCommit || run(['diff', '--name-only', '-z', 'HEAD', '--'], { raw: true }).split('\0').filter(Boolean).join('\0') !== paths.join('\0')) throw new Error('native_artifact_changed');
|
|
155
|
+
for (const { full, info } of observed) {
|
|
156
|
+
const current = await lstat(full);
|
|
157
|
+
if (current.ino !== info.ino || current.dev !== info.dev || current.mode !== info.mode
|
|
158
|
+
|| current.size !== info.size || current.mtimeMs !== info.mtimeMs) throw new Error('native_artifact_changed');
|
|
159
|
+
}
|
|
160
|
+
await indexSpelling(canonicalRoot, indexBefore);
|
|
161
|
+
if (JSON.stringify(indexBefore) !== JSON.stringify(indexState(run))) throw new Error('native_artifact_changed');
|
|
162
|
+
const body = { action: 'restore_native', silo_id: siloId, native: {
|
|
163
|
+
consent: true, replace: true, harness, session_id: sessionId, base_commit: baseCommit, transcript, files,
|
|
164
|
+
} };
|
|
165
|
+
if (Buffer.byteLength(transcript) + files.reduce((n, file) => n + Buffer.byteLength(file.text), 0) > MAX_BYTES) throw new Error('native_artifact_bound');
|
|
166
|
+
return body;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export async function continueCloud(options, request = apiClient({ timeoutMs: 80_000, deadline: Date.now() + 80_000 })) {
|
|
170
|
+
if (options.consent !== true || options.replace !== true) throw new Error('native_consent_required');
|
|
171
|
+
const body = await prepareContinuation(options);
|
|
172
|
+
const result = await request('/ade', body);
|
|
173
|
+
return { silo_id: result.silo_id, session_id: result.session_id, harness: result.harness,
|
|
174
|
+
native_command_started: result.native_command_started === true,
|
|
175
|
+
artifacts_verified: result.artifacts_verified === true, safe_to_close_laptop: false,
|
|
176
|
+
note: 'Native import requested. Authentication, context readiness and local/cloud handover still need verification.' };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export async function runNativeContinue(args, { isPublic } = {}) {
|
|
180
|
+
const names = { '--harness': 'harness', '--session': 'sessionId', '--transcript': 'transcriptPath', '--repo': 'cwd', '--silo': 'siloId' };
|
|
181
|
+
const options = {};
|
|
182
|
+
for (let i = 0; i < args.length; i++) {
|
|
183
|
+
if (args[i] === '--consent') options.consent = true;
|
|
184
|
+
else if (args[i] === '--replace') options.replace = true;
|
|
185
|
+
else if (args[i] === '--preserve-tree') options.preserveTree = true;
|
|
186
|
+
else if (args[i] === '--untracked' && args[i + 1]) {
|
|
187
|
+
options.selectedUntracked ||= []; options.selectedUntracked.push(args[++i]);
|
|
188
|
+
}
|
|
189
|
+
else if (names[args[i]] && args[i + 1]) {
|
|
190
|
+
const name = names[args[i]];
|
|
191
|
+
options[name] = args[++i];
|
|
192
|
+
}
|
|
193
|
+
else throw new Error('native_arguments');
|
|
194
|
+
}
|
|
195
|
+
// 🔴 This carries files to a box, and a box pushes what it finds. A public repository, or one nobody could read, gets none (`move.mjs`).
|
|
196
|
+
const repo = repository(options.cwd);
|
|
197
|
+
if (repo) {
|
|
198
|
+
const ask = isPublic ?? (await import('./move.mjs')).isPublicRepo;
|
|
199
|
+
if (await ask(repo.name) !== false) throw new Error('native_public_repository');
|
|
200
|
+
}
|
|
201
|
+
if (options.preserveTree) return (await import('./native-tree.mjs')).continueTreeCloud(options);
|
|
202
|
+
if (options.selectedUntracked) throw new Error('native_arguments');
|
|
203
|
+
return continueCloud(options);
|
|
204
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { lstat, realpath, opendir } from 'node:fs/promises';
|
|
2
|
+
import { resolve, relative, sep, isAbsolute } from 'node:path';
|
|
3
|
+
import { git } from './io.mjs';
|
|
4
|
+
|
|
5
|
+
const paths = text => text.split('\0').filter(Boolean);
|
|
6
|
+
const fingerprint = info => [info.dev, info.ino, info.mode, info.size, info.mtimeMs].join(':');
|
|
7
|
+
const inside = (root, path) => { const rel = relative(root, path); return rel && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel); };
|
|
8
|
+
|
|
9
|
+
export const nativeGit = cwd => (args, options = {}) => git(cwd, ['--literal-pathspecs',
|
|
10
|
+
'-c', 'core.fileMode=true', '-c', 'core.fsmonitor=false', '-c', 'core.untrackedCache=false', ...args], { strict: true, ...options });
|
|
11
|
+
|
|
12
|
+
export function indexState(run) {
|
|
13
|
+
// Git can suppress dirty paths using index flags. Refuse rather than read every
|
|
14
|
+
// tracked file (which could include credentials) to discover hidden changes.
|
|
15
|
+
const tagged = run(['ls-files', '--sparse', '-v', '-z'], { raw: true });
|
|
16
|
+
const debug = run(['ls-files', '--sparse', '--debug', '-z'], { raw: true });
|
|
17
|
+
const flags = [...debug.matchAll(/\tflags: ([0-9a-f]+)\n/g)];
|
|
18
|
+
const entries = paths(tagged);
|
|
19
|
+
const sparse = ['core.sparseCheckout', 'index.sparse'].map(key => run(['config', '--type=bool', '--default=false', '--get', key]));
|
|
20
|
+
if (entries.some(row => !row.startsWith('H ')) || flags.length !== entries.length
|
|
21
|
+
|| flags.some(row => Number.parseInt(row[1], 16) !== 0) || sparse.some(value => value !== 'false')) {
|
|
22
|
+
throw new Error('native_index_state_unsupported');
|
|
23
|
+
}
|
|
24
|
+
return { tagged, debug, sparse, ignoreCase: run(['config', '--type=bool', '--default=false', '--get', 'core.ignoreCase']) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const MAX_INDEX_PATHS = 10_000;
|
|
28
|
+
const MAX_INDEX_DIRECTORIES = 2_048;
|
|
29
|
+
const MAX_DIRECTORY_ENTRIES = 40_000;
|
|
30
|
+
const MAX_DIRECTORY_NAME_BYTES = 2 * 1024 * 1024;
|
|
31
|
+
|
|
32
|
+
async function directoryNames(root, full, budget) {
|
|
33
|
+
if ((full !== root && !inside(root, full)) || await realpath(full) !== full) throw new Error('native_reserved_path');
|
|
34
|
+
const before = await lstat(full);
|
|
35
|
+
if (!before.isDirectory() || before.isSymbolicLink()) throw new Error('native_reserved_path');
|
|
36
|
+
const handle = await opendir(full), exact = new Set(), folded = new Set();
|
|
37
|
+
try {
|
|
38
|
+
for (let n = 0; n <= MAX_DIRECTORY_ENTRIES; n++) {
|
|
39
|
+
const entry = await handle.read();
|
|
40
|
+
if (!entry) break;
|
|
41
|
+
budget.count++; budget.bytes += Buffer.byteLength(entry.name);
|
|
42
|
+
if (budget.count > MAX_DIRECTORY_ENTRIES || budget.bytes > MAX_DIRECTORY_NAME_BYTES) throw new Error('native_artifact_bound');
|
|
43
|
+
exact.add(entry.name); folded.add(entry.name.normalize('NFC').toLowerCase());
|
|
44
|
+
}
|
|
45
|
+
} finally { await handle.close(); }
|
|
46
|
+
const after = await lstat(full);
|
|
47
|
+
if (fingerprint(before) !== fingerprint(after) || await realpath(full) !== full) throw new Error('native_artifact_changed');
|
|
48
|
+
return { exact, folded };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function indexSpelling(root, index) {
|
|
52
|
+
const entries = paths(index.tagged);
|
|
53
|
+
if (entries.length > MAX_INDEX_PATHS) throw new Error('native_artifact_bound');
|
|
54
|
+
const directories = new Map(), budget = { count: 0, bytes: 0 };
|
|
55
|
+
for (const row of entries) {
|
|
56
|
+
const parts = row.slice(2).split('/');
|
|
57
|
+
if (parts.length > 64 || Buffer.byteLength(row) > 4096 || parts.some(part => !part || part === '.' || part === '..')) throw new Error('native_reserved_path');
|
|
58
|
+
let directory = root;
|
|
59
|
+
for (let n = 0; n < parts.length; n++) {
|
|
60
|
+
if (!directories.has(directory)) {
|
|
61
|
+
if (directories.size >= MAX_INDEX_DIRECTORIES) throw new Error('native_artifact_bound');
|
|
62
|
+
directories.set(directory, await directoryNames(root, directory, budget));
|
|
63
|
+
}
|
|
64
|
+
const names = directories.get(directory), part = parts[n];
|
|
65
|
+
if (!names.exact.has(part)) {
|
|
66
|
+
if (names.folded.has(part.normalize('NFC').toLowerCase())) throw new Error('native_index_state_unsupported');
|
|
67
|
+
// Filesystems may equate other spellings too; an existing non-exact alias
|
|
68
|
+
// is not a proven deletion, even when Git's case setting says otherwise.
|
|
69
|
+
try { await lstat(resolve(directory, part)); throw new Error('native_index_state_unsupported'); }
|
|
70
|
+
catch (error) { if (!['ENOENT', 'ENOTDIR'].includes(error.code)) throw error; }
|
|
71
|
+
break; // A real deletion is represented by the normal Git path list.
|
|
72
|
+
}
|
|
73
|
+
if (n < parts.length - 1) directory = resolve(directory, part);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { lstat, realpath } from 'node:fs/promises';
|
|
2
|
+
import { resolve, relative, sep, isAbsolute } from 'node:path';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { repository, apiClient } from './io.mjs';
|
|
5
|
+
import { nativeGit, indexState, indexSpelling } from './native-index.mjs';
|
|
6
|
+
import { redact } from './redact.mjs';
|
|
7
|
+
import { NATIVE_LIMITS, checkNativeRelativePath, readNativeBytes, captureNativeTranscript } from './native-export.mjs';
|
|
8
|
+
|
|
9
|
+
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
10
|
+
const paths = text => text.split('\0').filter(Boolean);
|
|
11
|
+
const fingerprint = info => info ? [info.dev, info.ino, info.mode, info.size, info.mtimeMs].join(':') : 'absent';
|
|
12
|
+
const inside = (root, path) => { const rel = relative(root, path); return rel && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel); };
|
|
13
|
+
|
|
14
|
+
async function worktreeFile(root, path) {
|
|
15
|
+
let full = root;
|
|
16
|
+
const parts = path.split('/');
|
|
17
|
+
for (let i = 0; i < parts.length; i++) {
|
|
18
|
+
full = resolve(full, parts[i]);
|
|
19
|
+
let info;
|
|
20
|
+
try { info = await lstat(full); }
|
|
21
|
+
catch (error) { if (error.code === 'ENOENT') return { info: null, blob: null }; throw error; }
|
|
22
|
+
if (info.isSymbolicLink() || (i < parts.length - 1 && !info.isDirectory())) throw new Error('native_reserved_path');
|
|
23
|
+
if (i === parts.length - 1 && !info.isFile()) throw new Error('native_file_mode_unsupported');
|
|
24
|
+
}
|
|
25
|
+
const canonical = await realpath(full);
|
|
26
|
+
if (!inside(root, canonical) || canonical !== full) throw new Error('native_reserved_path');
|
|
27
|
+
const info = await lstat(full);
|
|
28
|
+
const bytes = await readNativeBytes(full, NATIVE_LIMITS.blobBytes, info);
|
|
29
|
+
return { info, blob: blob(bytes, info.mode & 0o111 ? '100755' : '100644') };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function blob(bytes, mode) {
|
|
33
|
+
if (bytes.length > NATIVE_LIMITS.blobBytes) throw new Error('native_artifact_bound');
|
|
34
|
+
const text = bytes.toString('utf8').replaceAll('\0', '');
|
|
35
|
+
if (redact(text) !== text || text.toLowerCase().replace(/[_-]/g, '').includes('sessiontoken')) throw new Error('native_source_credential_or_binary');
|
|
36
|
+
return { mode, base64: bytes.toString('base64'), sha256: sha(bytes) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function indexFile(run, path) {
|
|
40
|
+
const raw = run(['ls-files', '--stage', '-z', '--', path], { raw: true });
|
|
41
|
+
if (!raw) return { raw, blob: null };
|
|
42
|
+
const rows = paths(raw);
|
|
43
|
+
const match = /^(100644|100755) ([a-f0-9]{40}) 0\t([\s\S]+)$/.exec(rows[0]);
|
|
44
|
+
if (rows.length !== 1 || !match || match[3] !== path) throw new Error('native_file_mode_unsupported');
|
|
45
|
+
const sizeText = run(['cat-file', '-s', match[2]]);
|
|
46
|
+
const size = Number(sizeText);
|
|
47
|
+
if (!/^[0-9]+$/.test(sizeText) || !Number.isSafeInteger(size) || size < 0 || size > NATIVE_LIMITS.blobBytes) throw new Error('native_artifact_bound');
|
|
48
|
+
const bytes = run(['cat-file', 'blob', match[2]], { bytes: true });
|
|
49
|
+
return { raw, blob: blob(bytes, match[1]) };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function state(run) {
|
|
53
|
+
return { index: indexState(run), head: run(['rev-parse', 'HEAD']),
|
|
54
|
+
dirty: run(['diff', '--no-renames', '--name-only', '-z', 'HEAD', '--'], { raw: true }),
|
|
55
|
+
staged: run(['diff', '--cached', '--no-renames', '--name-only', '-z', 'HEAD', '--'], { raw: true }),
|
|
56
|
+
untracked: run(['ls-files', '--others', '--exclude-standard', '-z'], { raw: true }) };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function captureTree({ cwd, selectedUntracked = [] }) {
|
|
60
|
+
const repo = repository(cwd);
|
|
61
|
+
if (!repo) throw new Error('native_repository');
|
|
62
|
+
const root = await realpath(repo.root);
|
|
63
|
+
const run = nativeGit(repo.root);
|
|
64
|
+
const before = state(run);
|
|
65
|
+
await indexSpelling(root, before.index);
|
|
66
|
+
if (!/^[a-f0-9]{40}$/.test(before.head)) throw new Error('native_repository');
|
|
67
|
+
const untracked = paths(before.untracked).sort();
|
|
68
|
+
if (!Array.isArray(selectedUntracked) || selectedUntracked.some(path => typeof path !== 'string')
|
|
69
|
+
|| [...new Set(selectedUntracked)].sort().join('\0') !== untracked.join('\0')) throw new Error('native_untracked_selection_required');
|
|
70
|
+
const names = [...new Set([...paths(before.dirty), ...paths(before.staged), ...untracked])].sort();
|
|
71
|
+
if (names.length > NATIVE_LIMITS.files) throw new Error('native_artifact_bound');
|
|
72
|
+
names.forEach(checkNativeRelativePath);
|
|
73
|
+
if (names.some((path, i) => names.some((other, j) => i !== j && other.startsWith(path + '/')))) throw new Error('native_reserved_path');
|
|
74
|
+
const tree = [], observed = [];
|
|
75
|
+
let total = 0;
|
|
76
|
+
for (const path of names) {
|
|
77
|
+
const index = indexFile(run, path), worktree = await worktreeFile(root, path);
|
|
78
|
+
const entry = { path, index: index.blob, worktree: worktree.blob };
|
|
79
|
+
total += [entry.index, entry.worktree].reduce((n, value) => n + (value ? Buffer.from(value.base64, 'base64').length : 0), 0);
|
|
80
|
+
if (total > NATIVE_LIMITS.bytes) throw new Error('native_artifact_bound');
|
|
81
|
+
tree.push(entry); observed.push({ path, index: index.raw, worktree });
|
|
82
|
+
}
|
|
83
|
+
const after = state(run);
|
|
84
|
+
if (JSON.stringify(before) !== JSON.stringify(after)) throw new Error('native_artifact_changed');
|
|
85
|
+
for (const row of observed) {
|
|
86
|
+
const current = await worktreeFile(root, row.path);
|
|
87
|
+
if (indexFile(run, row.path).raw !== row.index || fingerprint(current.info) !== fingerprint(row.worktree.info)
|
|
88
|
+
|| current.blob?.sha256 !== row.worktree.blob?.sha256) throw new Error('native_artifact_changed');
|
|
89
|
+
}
|
|
90
|
+
await indexSpelling(root, before.index);
|
|
91
|
+
if (JSON.stringify(before) !== JSON.stringify(state(run))) throw new Error('native_artifact_changed');
|
|
92
|
+
return { repo: repo.name, baseCommit: before.head, tree, bytes: total };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function prepareTreeContinuation(options) {
|
|
96
|
+
if (options.consent !== true || options.replace !== true) throw new Error('native_consent_required');
|
|
97
|
+
if (!options.siloId) throw new Error('native_selection');
|
|
98
|
+
const captured = await captureTree(options);
|
|
99
|
+
const transcript = await captureNativeTranscript(options);
|
|
100
|
+
if (captured.bytes + Buffer.byteLength(transcript) > NATIVE_LIMITS.bytes) throw new Error('native_artifact_bound');
|
|
101
|
+
return { action: 'restore_native', silo_id: options.siloId, native: { consent: true, replace: true,
|
|
102
|
+
harness: options.harness, session_id: options.sessionId, base_commit: captured.baseCommit, transcript,
|
|
103
|
+
tree: captured.tree } };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function continueTreeCloud(options, request = apiClient({ timeoutMs: 80_000, deadline: Date.now() + 80_000 })) {
|
|
107
|
+
if (options.consent !== true || options.replace !== true) throw new Error('native_consent_required');
|
|
108
|
+
const capabilities = await request('/ade', { action: 'native_capabilities' });
|
|
109
|
+
if (capabilities.transport_version !== 2 || capabilities.tree_snapshot !== true) throw new Error('native_tree_transport_unsupported');
|
|
110
|
+
const body = await prepareTreeContinuation(options);
|
|
111
|
+
const result = await request('/ade', body);
|
|
112
|
+
if (result.tree_snapshot_applied !== true || result.artifacts_verified !== true) throw new Error('native_restore_unverified');
|
|
113
|
+
if (result.silo_id !== options.siloId || result.session_id !== options.sessionId || result.harness !== options.harness) throw new Error('native_receipt_mismatch');
|
|
114
|
+
return { silo_id: result.silo_id, session_id: result.session_id, harness: result.harness,
|
|
115
|
+
artifacts_verified: true, tree_snapshot_applied: true, native_command_started: result.native_command_started === true,
|
|
116
|
+
safe_to_close_laptop: false, note: 'Selected index/worktree state was imported. Native authentication, context readiness and ownership handover still require verification.' };
|
|
117
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { mkdir, writeFile, opendir, lstat, rmdir, rename, link, unlink } from 'node:fs/promises';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { setTimeout } from 'node:timers/promises';
|
|
4
|
+
import { readJson, writeJson } from './io.mjs';
|
|
5
|
+
|
|
6
|
+
const CLAIM_ATTEMPTS = 20;
|
|
7
|
+
const CLAIM_RETRY_MS = 5;
|
|
8
|
+
|
|
9
|
+
export async function beginContext(path, turnId) {
|
|
10
|
+
const requestId = randomUUID();
|
|
11
|
+
await writeJson(path, { turnId, requestId, text: null });
|
|
12
|
+
return requestId;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function removeEmpty(path) {
|
|
16
|
+
for (let attempt = 0; attempt < CLAIM_ATTEMPTS; attempt++) {
|
|
17
|
+
try { await rmdir(path); return; }
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (['ENOENT', 'ENOTEMPTY', 'EEXIST'].includes(error.code)) return;
|
|
20
|
+
if (!['EPERM', 'EACCES'].includes(error.code) || attempt === CLAIM_ATTEMPTS - 1) throw error;
|
|
21
|
+
}
|
|
22
|
+
await setTimeout(CLAIM_RETRY_MS);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Idempotent cleanup only; dead-owner election must keep its raw unlink winner check.
|
|
27
|
+
async function unlinkOwned(path) {
|
|
28
|
+
for (let attempt = 0; attempt < CLAIM_ATTEMPTS; attempt++) {
|
|
29
|
+
try { await unlink(path); return; }
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (error.code === 'ENOENT') return;
|
|
32
|
+
if (!['EPERM', 'EACCES'].includes(error.code) || attempt === CLAIM_ATTEMPTS - 1) throw error;
|
|
33
|
+
}
|
|
34
|
+
await setTimeout(CLAIM_RETRY_MS);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function releaseGuard(path, owner) {
|
|
39
|
+
await unlinkOwned(`${path}/${owner}`);
|
|
40
|
+
await removeEmpty(path);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function guardOwner(path) {
|
|
44
|
+
const info = await lstat(path);
|
|
45
|
+
if (!info.isDirectory() || info.isSymbolicLink()) return null;
|
|
46
|
+
const directory = await opendir(path, { bufferSize: 2 });
|
|
47
|
+
const names = [];
|
|
48
|
+
// A legitimate published guard has exactly one regular PID+opaque-token owner.
|
|
49
|
+
// Look at at most two entries; never enumerate or recursively remove unknown state.
|
|
50
|
+
for await (const entry of directory) {
|
|
51
|
+
if (!entry.isFile() || entry.isSymbolicLink() || names.length === 1) return null;
|
|
52
|
+
names.push(entry.name);
|
|
53
|
+
}
|
|
54
|
+
if (!names.length) return '';
|
|
55
|
+
const match = /^([1-9][0-9]{0,9})\.([0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})\.owner$/.exec(names[0]);
|
|
56
|
+
if (!match || Number(match[1]) > 2_147_483_647) return null;
|
|
57
|
+
return { name: names[0], pid: Number(match[1]) };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function reclaimGuard(path) {
|
|
61
|
+
let owner;
|
|
62
|
+
try { owner = await guardOwner(path); }
|
|
63
|
+
catch (error) { if (error.code === 'ENOENT') return false; throw error; }
|
|
64
|
+
if (owner === '') { await removeEmpty(path); return true; }
|
|
65
|
+
if (!owner) return true;
|
|
66
|
+
try { process.kill(owner.pid, 0); return true; }
|
|
67
|
+
catch (error) { if (error.code !== 'ESRCH') return true; }
|
|
68
|
+
// Only the unlink winner may attempt rmdir. A stale observer must not remove
|
|
69
|
+
// a replacement owner; its different opaque filename makes this unlink fail.
|
|
70
|
+
try { await unlink(`${path}/${owner.name}`); }
|
|
71
|
+
catch (error) { if (error.code === 'ENOENT') return true; throw error; }
|
|
72
|
+
await removeEmpty(path);
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function guardedRename(path, claimed) {
|
|
77
|
+
const guard = `${path}.claim-lock`;
|
|
78
|
+
const token = randomUUID();
|
|
79
|
+
const candidate = `${path}.${token}.claim-candidate`;
|
|
80
|
+
const owner = `${process.pid}.${token}.owner`;
|
|
81
|
+
try { await mkdir(candidate, { mode: 0o700 }); }
|
|
82
|
+
catch (error) { if (error.code === 'ENOENT') return 'missing'; throw error; }
|
|
83
|
+
let published = false;
|
|
84
|
+
try {
|
|
85
|
+
await writeFile(`${candidate}/${owner}`, '', { flag: 'wx', mode: 0o600 });
|
|
86
|
+
try { await rename(candidate, guard); published = true; }
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (!['EEXIST', 'ENOTEMPTY', 'EPERM', 'EACCES'].includes(error.code)) throw error;
|
|
89
|
+
let observed;
|
|
90
|
+
try { observed = await reclaimGuard(guard); }
|
|
91
|
+
catch (inspectionError) {
|
|
92
|
+
if (!['EPERM', 'EACCES'].includes(inspectionError.code)) throw inspectionError;
|
|
93
|
+
return { error: inspectionError };
|
|
94
|
+
}
|
|
95
|
+
// Windows may report a conflict just before its owner removes the guard.
|
|
96
|
+
// Retry that ambiguity, but retain a persistent permission error verbatim.
|
|
97
|
+
if (!observed && ['EPERM', 'EACCES'].includes(error.code)) return { error };
|
|
98
|
+
return 'busy';
|
|
99
|
+
}
|
|
100
|
+
try { await rename(path, claimed); return 'claimed'; }
|
|
101
|
+
catch (error) { if (error.code !== 'ENOENT') throw error; return 'missing'; }
|
|
102
|
+
finally { await releaseGuard(guard, owner); }
|
|
103
|
+
} finally {
|
|
104
|
+
if (!published) {
|
|
105
|
+
await unlinkOwned(`${candidate}/${owner}`);
|
|
106
|
+
await removeEmpty(candidate);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function claim(path, retry) {
|
|
112
|
+
for (let attempt = 0; attempt < CLAIM_ATTEMPTS; attempt++) {
|
|
113
|
+
const claimed = `${path}.${randomUUID()}.claimed`;
|
|
114
|
+
const result = await guardedRename(path, claimed);
|
|
115
|
+
if (result === 'claimed') return claimed;
|
|
116
|
+
if (result === 'missing' && !retry) return null;
|
|
117
|
+
if (result === 'busy' && attempt === CLAIM_ATTEMPTS - 1) throw new Error('context_claim_locked');
|
|
118
|
+
if (typeof result === 'object' && attempt === CLAIM_ATTEMPTS - 1) throw result.error;
|
|
119
|
+
await setTimeout(CLAIM_RETRY_MS);
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function restore(claimed, path) {
|
|
125
|
+
// An intervening new prompt owns the path. Never replace it with an older result.
|
|
126
|
+
try { await link(claimed, path); return true; }
|
|
127
|
+
catch (error) { if (error.code !== 'EEXIST') throw error; return false; }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export async function saveContext(path, turnId, requestId, text, degraded = false) {
|
|
131
|
+
const claimed = await claim(path, true);
|
|
132
|
+
if (!claimed) return false;
|
|
133
|
+
try {
|
|
134
|
+
const stored = await readJson(claimed);
|
|
135
|
+
const matches = stored.turnId === turnId && stored.requestId === requestId;
|
|
136
|
+
if (matches) await writeJson(claimed, { ...stored, text, degraded: degraded === true });
|
|
137
|
+
const restored = await restore(claimed, path);
|
|
138
|
+
return matches && restored;
|
|
139
|
+
} finally { await unlink(claimed); }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function takeContext(path, turnId) {
|
|
143
|
+
if (!turnId) return null;
|
|
144
|
+
const claimed = await claim(path, false);
|
|
145
|
+
if (!claimed) return null;
|
|
146
|
+
try {
|
|
147
|
+
const stored = await readJson(claimed);
|
|
148
|
+
if (stored.turnId === turnId && typeof stored.text === 'string' && stored.text) return { text: stored.text, degraded: stored.degraded === true };
|
|
149
|
+
await restore(claimed, path);
|
|
150
|
+
return null;
|
|
151
|
+
} finally { await unlink(claimed); }
|
|
152
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Public CLI stream redaction port; see NOTICE for upstream attribution.
|
|
2
|
+
export const FIELD_CAP = 20_000;
|
|
3
|
+
const MAX_DEPTH = 24;
|
|
4
|
+
const MAX_FIELDS = 1_000;
|
|
5
|
+
const SENSITIVE_KEY = /(?:secret|token|password|api[_-]?key|private[_-]?key)/i;
|
|
6
|
+
export const isSensitiveField = key => SENSITIVE_KEY.test(key);
|
|
7
|
+
// Shapes that are a credential whatever surrounds them. Also the list the write hook warns on (credential.mjs).
|
|
8
|
+
export const VENDOR_PATTERNS = Object.freeze([
|
|
9
|
+
[/\b(?:sk|rk|pk)_(?:live|test)_[A-Za-z0-9]{8,}/g, '[redacted stripe key]'],
|
|
10
|
+
[/\bestelle_(?:live|test)_[A-Za-z0-9_-]{8,}/g, '[redacted estelle key]'],
|
|
11
|
+
[/\bgh[pousr]_[A-Za-z0-9]{20,}/g, '[redacted github token]'],
|
|
12
|
+
[/\bgithub_pat_[A-Za-z0-9_]{20,}/g, '[redacted github token]'],
|
|
13
|
+
[/\bsk-(?:proj-|ant-)?[A-Za-z0-9_-]{20,}/g, '[redacted API key]'],
|
|
14
|
+
[/\bxox[abprs]-[A-Za-z0-9-]{10,}/g, '[redacted slack token]'],
|
|
15
|
+
[/\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g, '[redacted jwt]'],
|
|
16
|
+
[/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g, '[redacted private key]'],
|
|
17
|
+
[/(postgres(?:ql)?:\/\/[^:\s/]+:)[^@\s]+@/g, '$1[redacted]@'],
|
|
18
|
+
[/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g, '[redacted aws key]'],
|
|
19
|
+
[/\bAIza[A-Za-z0-9_-]{35}(?![A-Za-z0-9_-])/g, '[redacted google key]'],
|
|
20
|
+
[/\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/g, '[redacted sendgrid key]'],
|
|
21
|
+
]);
|
|
22
|
+
const PATTERNS = [
|
|
23
|
+
...VENDOR_PATTERNS,
|
|
24
|
+
[/\b([A-Z0-9_]*(?:SECRET|TOKEN|PASSWORD|API_KEY|PRIVATE_KEY)[A-Z0-9_]*)\s*=\s*["']?[^\s"']{8,}/gi, '$1=[redacted]'],
|
|
25
|
+
[/\b(Bearer\s+)[A-Za-z0-9._~+\/-]{8,}=*/gi, '$1[redacted]'],
|
|
26
|
+
];
|
|
27
|
+
// Rooms proofs are opaque HMACs: their field label, not a vendor prefix, identifies them.
|
|
28
|
+
// Keep the delimiter so JSON snippets remain parseable after filtering. Bare values require
|
|
29
|
+
// 16 characters; a code identifier such as `session_token: undefined` is not a credential.
|
|
30
|
+
const LABELED_VALUE = /(["'`]?\b[A-Z0-9_-]{0,128}(?:SECRET|TOKEN|PASSWORD|API[_-]?KEY|PRIVATE[_-]?KEY)[A-Z0-9_-]{0,128}["'`]?\s*[:=]\s*)("(?:\\.|[^"\\\r\n]){8,}"|'(?:\\.|[^'\\\r\n]){8,}'|`[^`\r\n]{8,}`|[A-Za-z0-9._~+\/-]{16,}={0,2})(?=[\s,;)}\]<>]|$)/gi;
|
|
31
|
+
|
|
32
|
+
function redactLabeledValue(match, label, value) {
|
|
33
|
+
if (/^["'`]?\[redacted/i.test(value)) return match;
|
|
34
|
+
const quoted = ['"', "'", '`'].includes(value[0]);
|
|
35
|
+
if (quoted && value.slice(1, -1).startsWith('${')) return match;
|
|
36
|
+
const delimiter = quoted ? value[0] : '';
|
|
37
|
+
return `${label}${delimiter}[redacted named credential]${delimiter}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Credential-pattern filtering, not a guarantee that arbitrary secrets are recognizable. */
|
|
41
|
+
export function redact(text) {
|
|
42
|
+
if (typeof text !== 'string') throw new TypeError('redact expects text');
|
|
43
|
+
// Normalize NULs before matching: removing them afterwards could join two fragments into
|
|
44
|
+
// a recognizable credential only after the redaction pass had already finished.
|
|
45
|
+
return PATTERNS.reduce((out, [pattern, replacement]) => out.replace(pattern, replacement), text.replaceAll('\0', ''))
|
|
46
|
+
.replace(LABELED_VALUE, redactLabeledValue)
|
|
47
|
+
.replace(/(\bsession[_-]token\s+is\s+)(["'`]?)[a-f0-9]{32,128}\2(?=[\s.,:;)}\]<>]|$)/gi,
|
|
48
|
+
(_, label, quote) => `${label}${quote}[redacted named credential]${quote}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function cap(text) {
|
|
52
|
+
const clean = redact(text);
|
|
53
|
+
const chars = Array.from(clean);
|
|
54
|
+
if (chars.length <= FIELD_CAP) return clean;
|
|
55
|
+
return `${chars.slice(0, FIELD_CAP).join('')}\n… [truncated: ${chars.length - FIELD_CAP} more characters not shown]`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Bounded walk over JSON input. Redact nested strings and sensitive object values before spooling. */
|
|
59
|
+
export function sanitize(value, depth = 0) {
|
|
60
|
+
if (depth >= MAX_DEPTH) return '[truncated: nesting limit]';
|
|
61
|
+
if (typeof value === 'string') return cap(value);
|
|
62
|
+
if (Array.isArray(value)) {
|
|
63
|
+
const out = value.slice(0, MAX_FIELDS).map(item => sanitize(item, depth + 1));
|
|
64
|
+
if (value.length > MAX_FIELDS) out.push('[truncated: item limit]');
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
if (value && typeof value === 'object') {
|
|
68
|
+
const entries = Object.entries(value).slice(0, MAX_FIELDS).map(([key, item]) => [
|
|
69
|
+
cap(key), SENSITIVE_KEY.test(key) && item != null ? '[redacted]' : sanitize(item, depth + 1),
|
|
70
|
+
]);
|
|
71
|
+
return Object.fromEntries(entries);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|