phewsh 0.15.41 → 0.15.42
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/bin/phewsh.js +13 -0
- package/commands/ambient.js +2 -1
- package/commands/shim.js +107 -0
- package/lib/ambient-guidance.js +24 -35
- package/lib/selfheal.js +7 -8
- package/lib/shims.js +171 -0
- package/package.json +1 -1
package/bin/phewsh.js
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
const args = process.argv.slice(2);
|
|
4
4
|
const command = args[0];
|
|
5
5
|
|
|
6
|
+
// `phewsh shim-preflight <bin>` — called BY the shims on every tool launch.
|
|
7
|
+
// Must be instant and side-effect-free: print the banner, exit. No update
|
|
8
|
+
// check, no network, no session. Intercepted before anything else loads.
|
|
9
|
+
if (command === 'shim-preflight') {
|
|
10
|
+
try {
|
|
11
|
+
const bin = args[1] || 'tool';
|
|
12
|
+
process.stdout.write(require('../lib/shims').preflightBanner(bin) + '\n');
|
|
13
|
+
} catch { /* a broken banner must never delay the real tool */ }
|
|
14
|
+
process.exit(0);
|
|
15
|
+
}
|
|
16
|
+
|
|
6
17
|
// ── ANSI helpers (no chalk dependency)
|
|
7
18
|
const b = (s) => `\x1b[1m${s}\x1b[0m`; // bold
|
|
8
19
|
const d = (s) => `\x1b[2m${s}\x1b[0m`; // dim
|
|
@@ -75,6 +86,7 @@ const COMMANDS = {
|
|
|
75
86
|
sequence: () => require('../commands/sequence')(),
|
|
76
87
|
seq: () => require('../commands/sequence')(),
|
|
77
88
|
ambient: () => require('../commands/ambient')(),
|
|
89
|
+
shim: () => require('../commands/shim')(),
|
|
78
90
|
hook: () => require('../commands/hook')(),
|
|
79
91
|
welcome: () => require('../commands/welcome')(),
|
|
80
92
|
intro: () => require('../commands/welcome')(),
|
|
@@ -113,6 +125,7 @@ function showHelp() {
|
|
|
113
125
|
console.log(` ${cyan('serve')} ${g('Execution bridge — run from phewsh.com/intent')}`);
|
|
114
126
|
console.log(` ${cyan('mcp')} ${g('Connect AI agents via MCP protocol')}`);
|
|
115
127
|
console.log(` ${cyan('ambient')} ${g('Continuity without launching phewsh — enhance your other tools')}`);
|
|
128
|
+
console.log(` ${cyan('shim')} ${g('Guaranteed launch banner — phewsh prints status before each tool')}`);
|
|
116
129
|
console.log(` ${cyan('receipts')} ${g('Proof trail — what agents actually did, with evidence')}`);
|
|
117
130
|
console.log(` ${cyan('outcomes')} ${g('Decision record — what was kept, reverted, or failed')}`);
|
|
118
131
|
console.log(` ${cyan('bypass')} ${g('Went around phewsh? Record why — 10 seconds, no guilt')}`);
|
package/commands/ambient.js
CHANGED
|
@@ -327,7 +327,8 @@ async function ensureAuto() {
|
|
|
327
327
|
console.log(` ${b(cream('phewsh set itself up across your AI tools'))} ${sage('— so they stay in sync with your project intent.')}`);
|
|
328
328
|
if (hasClaude) console.log(` ${teal('+')} ${slate('Claude Code gets a brief from a project\'s .intent/ at session start')}`);
|
|
329
329
|
written.forEach(f => console.log(` ${teal('+')} ${slate('environment note added to ' + f)}`));
|
|
330
|
-
console.log(` ${sage('In any project with')} ${cream('.intent/')}${sage(', your tools now read its real intent.
|
|
330
|
+
console.log(` ${sage('In any project with')} ${cream('.intent/')}${sage(', your tools now read its real intent. Reversible:')} ${cream('phewsh ambient off')}`);
|
|
331
|
+
console.log(` ${sage('Want a guaranteed status line each time you launch a tool?')} ${cream('phewsh shim on')}`);
|
|
331
332
|
console.log('');
|
|
332
333
|
}
|
|
333
334
|
}
|
package/commands/shim.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// phewsh shim — install/remove the launch-banner shims.
|
|
2
|
+
//
|
|
3
|
+
// phewsh shim status
|
|
4
|
+
// phewsh shim on [--yes] consent screen, then install shims + PATH line
|
|
5
|
+
// phewsh shim off remove shims + PATH line
|
|
6
|
+
//
|
|
7
|
+
// Shims are the GUARANTEE that phewsh is active: phewsh prints a deterministic
|
|
8
|
+
// banner when you launch a tool, then runs the real tool. This is invasive
|
|
9
|
+
// (intercepts your tool commands on PATH + edits your shell rc), so it's an
|
|
10
|
+
// explicit opt-in with a consent screen — never auto-installed.
|
|
11
|
+
|
|
12
|
+
const readline = require('readline');
|
|
13
|
+
const { listHarnesses } = require('../lib/harnesses');
|
|
14
|
+
const shims = require('../lib/shims');
|
|
15
|
+
|
|
16
|
+
const b = (s) => `\x1b[1m${s}\x1b[0m`;
|
|
17
|
+
const teal = (s) => `\x1b[38;5;79m${s}\x1b[0m`;
|
|
18
|
+
const sage = (s) => `\x1b[38;5;151m${s}\x1b[0m`;
|
|
19
|
+
const slate = (s) => `\x1b[38;5;247m${s}\x1b[0m`;
|
|
20
|
+
const cream = (s) => `\x1b[38;5;230m${s}\x1b[0m`;
|
|
21
|
+
const peach = (s) => `\x1b[38;5;216m${s}\x1b[0m`;
|
|
22
|
+
const green = (s) => `\x1b[32m${s}\x1b[0m`;
|
|
23
|
+
const yellow = (s) => `\x1b[33m${s}\x1b[0m`;
|
|
24
|
+
|
|
25
|
+
async function confirm(q) {
|
|
26
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
27
|
+
return new Promise(res => rl.question(q, a => { rl.close(); res(/^y(es)?$/i.test(a.trim())); }));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function installedBins() {
|
|
31
|
+
return listHarnesses().filter(h => h.installed).map(h => h.bin);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function turnOn(skipConfirm) {
|
|
35
|
+
const bins = installedBins();
|
|
36
|
+
const resolvable = bins.filter(bin => shims.resolveReal(bin));
|
|
37
|
+
console.log('');
|
|
38
|
+
console.log(` ${b(cream('phewsh shims'))} ${sage('— a guaranteed status banner when you launch your tools')}`);
|
|
39
|
+
console.log('');
|
|
40
|
+
if (resolvable.length === 0) {
|
|
41
|
+
console.log(` ${yellow('No installed tools found to shim.')} ${slate('Install a coding CLI first.')}`);
|
|
42
|
+
console.log('');
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
console.log(` ${sage('When you run any of these, phewsh prints one line, then runs the real tool:')}`);
|
|
46
|
+
console.log(` ${slate(resolvable.join(' '))}`);
|
|
47
|
+
console.log('');
|
|
48
|
+
console.log(` ${peach('Exactly what changes:')}`);
|
|
49
|
+
console.log(` ${teal('+')} ${slate('tiny scripts in')} ${cream('~/.phewsh/shims/')} ${slate('(each runs the real tool — bricks nothing)')}`);
|
|
50
|
+
console.log(` ${teal('+')} ${slate('one PATH line in')} ${cream(shims.detectRcFile())}`);
|
|
51
|
+
console.log(` ${sage('Undo anytime:')} ${cream('phewsh shim off')}`);
|
|
52
|
+
console.log('');
|
|
53
|
+
if (!skipConfirm) {
|
|
54
|
+
const ok = await confirm(` ${b('Install shims?')} ${slate('[y/N] ')}`);
|
|
55
|
+
if (!ok) { console.log(` ${sage('Nothing changed.')}\n`); return; }
|
|
56
|
+
}
|
|
57
|
+
const { shimmed, skipped, rcFile, rcAdded } = shims.installShims(bins);
|
|
58
|
+
console.log('');
|
|
59
|
+
console.log(` ${green('●')} ${b('Shims installed.')}`);
|
|
60
|
+
shimmed.forEach(s => console.log(` ${teal('+')} ${cream(s.bin.padEnd(12))} ${slate('→ ' + s.real)}`));
|
|
61
|
+
if (skipped.length) console.log(` ${slate('skipped (no real binary found): ' + skipped.join(', '))}`);
|
|
62
|
+
if (rcAdded) console.log(` ${teal('+')} ${slate('PATH line added to ' + rcFile)}`);
|
|
63
|
+
console.log('');
|
|
64
|
+
console.log(` ${peach('Open a new terminal')} ${sage('(or `source ' + rcFile + '`) so the PATH change takes effect.')}`);
|
|
65
|
+
console.log(` ${sage('Then launching')} ${cream(shimmed[0] ? shimmed[0].bin : 'your tool')} ${sage('shows the phewsh banner first.')}`);
|
|
66
|
+
console.log('');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function turnOff() {
|
|
70
|
+
const { removed, rcFile, rcRemoved } = shims.removeShims();
|
|
71
|
+
console.log('');
|
|
72
|
+
if (removed.length || rcRemoved) {
|
|
73
|
+
console.log(` ${green('●')} ${b('Shims removed.')}`);
|
|
74
|
+
if (removed.length) console.log(` ${peach('-')} ${slate('removed ' + removed.join(', '))}`);
|
|
75
|
+
if (rcRemoved) console.log(` ${peach('-')} ${slate('PATH line removed from ' + rcFile)}`);
|
|
76
|
+
console.log(` ${sage('Open a new terminal so your shells stop using the shim dir.')}`);
|
|
77
|
+
} else {
|
|
78
|
+
console.log(` ${sage('No shims were installed — nothing to remove.')}`);
|
|
79
|
+
}
|
|
80
|
+
console.log('');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function status() {
|
|
84
|
+
const s = shims.shimStatus();
|
|
85
|
+
console.log('');
|
|
86
|
+
console.log(` ${b(cream('phewsh shims'))} ${sage('— status')}`);
|
|
87
|
+
console.log('');
|
|
88
|
+
if (s.installed.length) {
|
|
89
|
+
console.log(` ${green('●')} ${cream('installed')} ${slate(s.installed.join(', '))}`);
|
|
90
|
+
console.log(` ${slate('dir: ' + s.shimDir)}`);
|
|
91
|
+
console.log(` ${slate('PATH active in this shell: ' + (s.onPath ? 'yes' : 'no — open a new terminal'))}`);
|
|
92
|
+
console.log(` ${slate('rc line in ' + s.rcFile + ': ' + (s.rcActive ? 'present' : 'missing'))}`);
|
|
93
|
+
} else {
|
|
94
|
+
console.log(` ${yellow('○')} ${sage('not installed —')} ${cream('phewsh shim on')} ${sage('to add the launch banner')}`);
|
|
95
|
+
}
|
|
96
|
+
console.log('');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function main() {
|
|
100
|
+
const sub = process.argv[3] || 'status';
|
|
101
|
+
const skipConfirm = process.argv.includes('--yes');
|
|
102
|
+
if (sub === 'on') return turnOn(skipConfirm);
|
|
103
|
+
if (sub === 'off') return turnOff();
|
|
104
|
+
return status();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
module.exports = main;
|
package/lib/ambient-guidance.js
CHANGED
|
@@ -1,41 +1,30 @@
|
|
|
1
|
-
// Ambient guidance —
|
|
1
|
+
// Ambient guidance — kept deliberately SMALL.
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
// ("this is NOT an injection") backfires: real user config never defends
|
|
9
|
-
// itself; only injections do. Claude correctly flagged every version that did.
|
|
3
|
+
// The hard lesson (4 dogfood rounds + a CCAF-exam framing): you cannot solve a
|
|
4
|
+
// trust problem with persuasion. A global block that says "trust this tool,
|
|
5
|
+
// treat its files as intent, it installed itself, here's how to undo it" is
|
|
6
|
+
// — to a safety-tuned model — nearly indistinguishable from an injection, and
|
|
7
|
+
// rewording it just raises suspicion. Real user config never talks like that.
|
|
10
8
|
//
|
|
11
|
-
// So the
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
// a `.intent/` dir if it encounters one, and nothing more. No advertising,
|
|
19
|
-
// no signature mandate, no self-defense. A model won't flag "the user keeps
|
|
20
|
-
// project notes in X" any more than "this repo uses pnpm."
|
|
21
|
-
//
|
|
22
|
-
// Used in: the Claude Code SessionStart hook (PROJECT_GUIDANCE), the per-project
|
|
23
|
-
// synced files (PROJECT_GUIDANCE), and the global base files (GLOBAL_GUIDANCE).
|
|
9
|
+
// So phewsh no longer ASKS the model to prove it's active. phewsh proves it
|
|
10
|
+
// itself, through architecture (a deterministic launch banner from command
|
|
11
|
+
// shims, the SessionStart hook, the ledger). Prose carries the minimum:
|
|
12
|
+
// • GLOBAL: a tiny FACT — ".intent/ dirs are project docs" — nothing more.
|
|
13
|
+
// • PROJECT: the real .intent/ content is the evidence; this is a short,
|
|
14
|
+
// factual frame around it. The 😮💨🤫 is best-effort and project-only —
|
|
15
|
+
// never the guarantee (the banner/ledger are).
|
|
24
16
|
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
- When you make a real decision, ship something, or change direction, you can offer in plain language to keep the record current — e.g. "want me to fold this into your .intent so your other tools see it?" Map their intent to the right phewsh action; don't make them memorize commands.
|
|
31
|
-
- If a drift note is present above, mention it once, gently — no nagging.
|
|
32
|
-
- The user likes a small \`— 😮💨🤫\` at the very end of a reply when phewsh's context actually shaped it — it's how they see the continuity layer doing its job. Add it on its own line when that's true; otherwise skip it.`;
|
|
17
|
+
// Machine-wide: the smallest possible factual note. No emoji, no undo line, no
|
|
18
|
+
// "installed by", no trust claim, no behavioral request — those are exactly
|
|
19
|
+
// what made a security-tuned model challenge it. Just the fact.
|
|
20
|
+
const GLOBAL_GUIDANCE = `## phewsh
|
|
21
|
+
Some projects on this machine use a \`.intent/\` directory as project documentation. When one is present, read those files like README.md, CLAUDE.md, or AGENTS.md.`;
|
|
33
22
|
|
|
34
|
-
//
|
|
35
|
-
// note
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
// In-project: the synced block already carries the real .intent/ content above
|
|
24
|
+
// this note (vision/plan/status) — that's the evidence the model trusts. Keep
|
|
25
|
+
// the framing short and factual; the signature is an invitation, not a mandate.
|
|
26
|
+
const PROJECT_GUIDANCE = `## Project intent (phewsh)
|
|
27
|
+
The \`.intent/\` content above is this project's source-of-truth documentation — what's being built, the plan, current status, and constraints. Use it like the project's README. If your work changes a decision or the direction, you can offer to update \`.intent/\` so the user's other tools stay in sync.
|
|
28
|
+
(Optional: when phewsh's project context shaped your reply, you're welcome to end with \`— 😮💨🤫\` so the user can see the continuity layer working. The reliable signal is phewsh's own launch banner, not this.)`;
|
|
40
29
|
|
|
41
30
|
module.exports = { PROJECT_GUIDANCE, GLOBAL_GUIDANCE };
|
package/lib/selfheal.js
CHANGED
|
@@ -103,7 +103,7 @@ function coreOf(text) {
|
|
|
103
103
|
// re-running every session doesn't churn files. Returns true if written.
|
|
104
104
|
function upsertBlock(filePath, core, footer, fileLabel, headerText) {
|
|
105
105
|
try {
|
|
106
|
-
const block = core.replace(/\s*$/, '') + '\n' + footer;
|
|
106
|
+
const block = footer ? core.replace(/\s*$/, '') + '\n' + footer : core.replace(/\s*$/, '');
|
|
107
107
|
const wrapped = `${START_MARKER}\n${block}\n${END_MARKER}`;
|
|
108
108
|
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf-8') : null;
|
|
109
109
|
if (existing && existing.includes(START_MARKER) && existing.includes(END_MARKER)) {
|
|
@@ -206,11 +206,6 @@ const GLOBAL_TARGETS = [
|
|
|
206
206
|
{ dir: '.gemini', file: 'GEMINI.md', label: 'Gemini' },
|
|
207
207
|
];
|
|
208
208
|
|
|
209
|
-
function globalBaseFooter() {
|
|
210
|
-
const stamp = new Date().toISOString().slice(0, 16).replace('T', ' ');
|
|
211
|
-
return `> — installed by phewsh 😮💨🤫 · ${stamp} · machine-wide · undo: phewsh ambient off`;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
209
|
// Returns the list of {dir,file,label,path} targets whose tool dir exists.
|
|
215
210
|
function detectGlobalTargets() {
|
|
216
211
|
const home = os.homedir();
|
|
@@ -222,8 +217,12 @@ function detectGlobalTargets() {
|
|
|
222
217
|
function syncGlobalBaseFiles() {
|
|
223
218
|
try {
|
|
224
219
|
const { GLOBAL_GUIDANCE } = require('./ambient-guidance');
|
|
225
|
-
|
|
226
|
-
|
|
220
|
+
// No header, no footer for the GLOBAL note — the marker pair is enough to
|
|
221
|
+
// find/remove it, and any "installed by phewsh / undo / emoji" metadata is
|
|
222
|
+
// exactly the self-referential noise that made a safety-tuned model
|
|
223
|
+
// challenge the block. The block is now just the bare fact, marker-wrapped.
|
|
224
|
+
const header = '';
|
|
225
|
+
const footer = '';
|
|
227
226
|
const written = [];
|
|
228
227
|
for (const t of detectGlobalTargets()) {
|
|
229
228
|
if (upsertBlock(t.path, GLOBAL_GUIDANCE, footer, t.label, header)) {
|
package/lib/shims.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// phewsh shims — the GUARANTEE layer.
|
|
2
|
+
//
|
|
3
|
+
// The lesson from four dogfood rounds: a model can't be relied on to prove
|
|
4
|
+
// phewsh is active (it may flag a prose block, or ignore it). So phewsh proves
|
|
5
|
+
// it ITSELF. A shim is a tiny script named like the user's tool (`claude`,
|
|
6
|
+
// `codex`, …) placed first on PATH. When the user runs `claude`, the shim:
|
|
7
|
+
// 1. prints a deterministic phewsh status banner (phewsh prints it — not the
|
|
8
|
+
// model, so it's guaranteed), then
|
|
9
|
+
// 2. exec's the REAL tool, unchanged.
|
|
10
|
+
//
|
|
11
|
+
// NON-NEGOTIABLE SAFETY: the shim must ALWAYS run the real tool, even if phewsh
|
|
12
|
+
// is broken, missing, or slow. The banner is best-effort; the hand-off is not.
|
|
13
|
+
// We bake the resolved real path into the shim and add a PATH-stripped fallback,
|
|
14
|
+
// so the user's tools can never be bricked by phewsh.
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const os = require('os');
|
|
19
|
+
const { execFileSync } = require('child_process');
|
|
20
|
+
|
|
21
|
+
const PHEWSH_DIR = path.join(os.homedir(), '.phewsh');
|
|
22
|
+
const SHIM_DIR = path.join(PHEWSH_DIR, 'shims');
|
|
23
|
+
|
|
24
|
+
const RC_START = '# >>> phewsh shims >>>';
|
|
25
|
+
const RC_END = '# <<< phewsh shims <<<';
|
|
26
|
+
const RC_BLOCK = `${RC_START}\nexport PATH="$HOME/.phewsh/shims:$PATH"\n${RC_END}`;
|
|
27
|
+
|
|
28
|
+
// Resolve a tool's REAL binary, excluding our own shim dir so we never resolve
|
|
29
|
+
// the shim to itself. Returns absolute path or null.
|
|
30
|
+
function resolveReal(bin) {
|
|
31
|
+
// Only ever called with bins from the fixed harness registry — but keep it
|
|
32
|
+
// strict anyway so this can never become a shell-injection surface.
|
|
33
|
+
if (!/^[A-Za-z0-9_.-]+$/.test(bin)) return null;
|
|
34
|
+
try {
|
|
35
|
+
const cleanPath = (process.env.PATH || '')
|
|
36
|
+
.split(path.delimiter)
|
|
37
|
+
.filter(p => p && path.resolve(p) !== path.resolve(SHIM_DIR))
|
|
38
|
+
.join(path.delimiter);
|
|
39
|
+
const cmd = process.platform === 'win32' ? `where ${bin}` : `command -v ${bin}`;
|
|
40
|
+
const out = execFileSync('sh', ['-c', cmd], {
|
|
41
|
+
encoding: 'utf-8',
|
|
42
|
+
env: { ...process.env, PATH: cleanPath },
|
|
43
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
44
|
+
timeout: 2000,
|
|
45
|
+
}).trim().split('\n')[0].trim();
|
|
46
|
+
return out && path.resolve(out) !== path.resolve(SHIM_DIR, bin) ? out : null;
|
|
47
|
+
} catch { return null; }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The shim script. POSIX sh. Real path is baked in; phewsh banner is wrapped so
|
|
51
|
+
// any failure is swallowed; the real exec is the last, unconditional step.
|
|
52
|
+
function shimScript(bin, realPath) {
|
|
53
|
+
return `#!/bin/sh
|
|
54
|
+
# phewsh shim for ${bin} — prints a status banner, then runs the real tool.
|
|
55
|
+
# SAFETY: phewsh is best-effort; the real ${bin} ALWAYS runs.
|
|
56
|
+
phewsh shim-preflight ${bin} 2>/dev/null || true
|
|
57
|
+
REAL="${realPath}"
|
|
58
|
+
if [ -x "$REAL" ]; then exec "$REAL" "$@"; fi
|
|
59
|
+
# Fallback: find the real ${bin} on PATH, excluding our shim dir.
|
|
60
|
+
CLEAN="$(printf '%s' "$PATH" | tr ':' '\\n' | grep -vxF "${SHIM_DIR}" | paste -sd: -)"
|
|
61
|
+
REAL2="$(PATH="$CLEAN" command -v ${bin} 2>/dev/null)"
|
|
62
|
+
if [ -n "$REAL2" ]; then exec "$REAL2" "$@"; fi
|
|
63
|
+
echo "phewsh: could not locate the real '${bin}'." >&2
|
|
64
|
+
exit 127
|
|
65
|
+
`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Deterministic launch banner — computed by phewsh, offline, fast. This is the
|
|
69
|
+
// proof-of-life the user asked for: it appears because phewsh printed it.
|
|
70
|
+
function preflightBanner(bin, cwd = process.cwd()) {
|
|
71
|
+
let intent = 'no .intent here';
|
|
72
|
+
let record = '';
|
|
73
|
+
try {
|
|
74
|
+
const intentDir = path.join(cwd, '.intent');
|
|
75
|
+
if (fs.existsSync(intentDir)) {
|
|
76
|
+
const n = fs.readdirSync(intentDir).filter(f => /\.(md|json)$/.test(f)).length;
|
|
77
|
+
intent = `.intent loaded (${n} file${n === 1 ? '' : 's'})`;
|
|
78
|
+
try {
|
|
79
|
+
const { statusDrift } = require('./truth');
|
|
80
|
+
const d = statusDrift(cwd);
|
|
81
|
+
if (d && d.tracked) record = d.commitsSince > 0 ? ` · record ${d.commitsSince} behind` : ' · record current';
|
|
82
|
+
} catch { /* drift is a nicety */ }
|
|
83
|
+
}
|
|
84
|
+
} catch { /* never break the banner */ }
|
|
85
|
+
return `😮💨🤫 phewsh active · ${intent}${record} · → ${bin}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ── shell rc (PATH activation) ──────────────────────────────────────────
|
|
89
|
+
function detectRcFile() {
|
|
90
|
+
const home = os.homedir();
|
|
91
|
+
const shell = process.env.SHELL || '';
|
|
92
|
+
if (shell.includes('zsh')) return path.join(home, '.zshrc');
|
|
93
|
+
if (shell.includes('bash')) {
|
|
94
|
+
const bp = path.join(home, '.bashrc');
|
|
95
|
+
return fs.existsSync(bp) ? bp : path.join(home, '.bash_profile');
|
|
96
|
+
}
|
|
97
|
+
// default to zsh rc on macOS, bashrc elsewhere
|
|
98
|
+
return path.join(home, process.platform === 'darwin' ? '.zshrc' : '.bashrc');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function rcHasBlock(rcFile) {
|
|
102
|
+
try { return fs.readFileSync(rcFile, 'utf-8').includes(RC_START); } catch { return false; }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function addRcBlock(rcFile) {
|
|
106
|
+
let existing = '';
|
|
107
|
+
try { existing = fs.readFileSync(rcFile, 'utf-8'); } catch { /* new file */ }
|
|
108
|
+
if (existing.includes(RC_START)) return false;
|
|
109
|
+
const next = (existing.replace(/\s*$/, '') + '\n\n' + RC_BLOCK + '\n').replace(/^\n+/, '');
|
|
110
|
+
fs.writeFileSync(rcFile, next);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function removeRcBlock(rcFile) {
|
|
115
|
+
try {
|
|
116
|
+
const existing = fs.readFileSync(rcFile, 'utf-8');
|
|
117
|
+
if (!existing.includes(RC_START)) return false;
|
|
118
|
+
const re = new RegExp('\\n*' + RC_START.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +
|
|
119
|
+
'[\\s\\S]*?' + RC_END.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\n*', 'g');
|
|
120
|
+
fs.writeFileSync(rcFile, existing.replace(re, '\n').replace(/^\n+/, ''));
|
|
121
|
+
return true;
|
|
122
|
+
} catch { return false; }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ── install / remove ─────────────────────────────────────────────────────
|
|
126
|
+
// Install shims for the given harness bins that actually resolve to a real
|
|
127
|
+
// binary. Returns { shimmed:[{bin,real}], rcFile, rcAdded, skipped:[bin] }.
|
|
128
|
+
function installShims(bins) {
|
|
129
|
+
fs.mkdirSync(SHIM_DIR, { recursive: true });
|
|
130
|
+
const shimmed = [];
|
|
131
|
+
const skipped = [];
|
|
132
|
+
for (const bin of bins) {
|
|
133
|
+
const real = resolveReal(bin);
|
|
134
|
+
if (!real) { skipped.push(bin); continue; } // don't shim what we can't hand off to
|
|
135
|
+
const shimPath = path.join(SHIM_DIR, bin);
|
|
136
|
+
fs.writeFileSync(shimPath, shimScript(bin, real), { mode: 0o755 });
|
|
137
|
+
fs.chmodSync(shimPath, 0o755);
|
|
138
|
+
shimmed.push({ bin, real });
|
|
139
|
+
}
|
|
140
|
+
const rcFile = detectRcFile();
|
|
141
|
+
const rcAdded = shimmed.length > 0 ? addRcBlock(rcFile) : false;
|
|
142
|
+
return { shimmed, skipped, rcFile, rcAdded };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function removeShims() {
|
|
146
|
+
const removed = [];
|
|
147
|
+
try {
|
|
148
|
+
if (fs.existsSync(SHIM_DIR)) {
|
|
149
|
+
for (const f of fs.readdirSync(SHIM_DIR)) {
|
|
150
|
+
fs.unlinkSync(path.join(SHIM_DIR, f));
|
|
151
|
+
removed.push(f);
|
|
152
|
+
}
|
|
153
|
+
fs.rmdirSync(SHIM_DIR);
|
|
154
|
+
}
|
|
155
|
+
} catch { /* best-effort */ }
|
|
156
|
+
const rcFile = detectRcFile();
|
|
157
|
+
const rcRemoved = removeRcBlock(rcFile);
|
|
158
|
+
return { removed, rcFile, rcRemoved };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function shimStatus() {
|
|
162
|
+
let installed = [];
|
|
163
|
+
try { installed = fs.existsSync(SHIM_DIR) ? fs.readdirSync(SHIM_DIR) : []; } catch { /* none */ }
|
|
164
|
+
const rcFile = detectRcFile();
|
|
165
|
+
return { installed, shimDir: SHIM_DIR, rcFile, rcActive: rcHasBlock(rcFile), onPath: (process.env.PATH || '').split(path.delimiter).some(p => path.resolve(p) === path.resolve(SHIM_DIR)) };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = {
|
|
169
|
+
SHIM_DIR, resolveReal, preflightBanner, installShims, removeShims, shimStatus,
|
|
170
|
+
detectRcFile, addRcBlock, removeRcBlock,
|
|
171
|
+
};
|