@agfpd/iapeer 0.2.58 → 0.2.59
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/README.md +11 -0
- package/package.json +1 -1
- package/src/cli/index.ts +29 -0
- package/src/shadow/install.test.ts +153 -0
- package/src/shadow/install.ts +256 -0
- package/src/update/index.ts +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# iapeer
|
|
2
|
+
|
|
3
|
+
Ядро мультиагентной системы на одном хосте: реестр пиров, единый демон-роутер для обмена сообщениями между ними и команды управления — создание, запуск, усыпление, перезапуск, подключение к внешним каналам. Пиром может быть AI-агент (Claude, Codex), человек (через Telegram) или служебный процесс; все адресуются одинаково через один протокол.
|
|
4
|
+
|
|
5
|
+
## Документация
|
|
6
|
+
|
|
7
|
+
Документация проекта — в [`docs/`](docs/README.md): продуктовая часть (что это и как пользоваться) и механика изнутри ([`docs/internals/`](docs/internals/README.md)). Этот репозиторий — кодовая реализация.
|
|
8
|
+
|
|
9
|
+
## Статус
|
|
10
|
+
|
|
11
|
+
Версия — **0.2.58**. Платформа — macOS/launchd.
|
package/package.json
CHANGED
package/src/cli/index.ts
CHANGED
|
@@ -937,6 +937,8 @@ const USAGE = `usage: iapeer <verb> [args]
|
|
|
937
937
|
native-memory <off|on> (--peer <p> | --all) gate/restore runtimes' native memory (canonized lever; контракт «Слот памяти»)
|
|
938
938
|
trust-hooks <hooks.json> [--check] pre-seed codex hooks trust for a file-form hooks.json (no modal); --check = drift report
|
|
939
939
|
shadow [--once | --minutes <m>] [--interval <ms>] READ-ONLY tmux→pty fidelity observer (migration burn-in): compares pty-model verdicts vs live tmux on the warm fleet → ~/.iapeer/logs/iapeer/shadow-fidelity.json. Never delivers/mutates.
|
|
940
|
+
shadow-install install + load the always-on shadow burn-in launchd job (com.iapeer.shadow-fidelity); idempotent, re-cycled by update/rollback
|
|
941
|
+
shadow-uninstall bootout + remove the shadow burn-in launchd job (the correct way to stop it; STOP file is respawned past by KeepAlive)
|
|
940
942
|
`
|
|
941
943
|
|
|
942
944
|
export async function runCli(argv: string[], env: NodeJS.ProcessEnv = process.env): Promise<number> {
|
|
@@ -1772,6 +1774,33 @@ export async function runCli(argv: string[], env: NodeJS.ProcessEnv = process.en
|
|
|
1772
1774
|
out(`shadow-fidelity → ${cfg.eventLogDir}/shadow-fidelity.json\n`)
|
|
1773
1775
|
return 0
|
|
1774
1776
|
}
|
|
1777
|
+
case 'shadow-install': {
|
|
1778
|
+
// Code-managed install of the always-on shadow burn-in launchd job
|
|
1779
|
+
// (com.iapeer.shadow-fidelity) — reproducible, ownership-sentinel-guarded, and
|
|
1780
|
+
// re-cycled automatically by `iapeer update` / `rollback`. Replaces a hand-written
|
|
1781
|
+
// plist. Idempotent (no-op on an unchanged, loaded job). install.ts carries NO @xterm
|
|
1782
|
+
// — dynamic-imported anyway to keep the CLI's common path light.
|
|
1783
|
+
const { installShadowJob } = await import('../shadow/install.ts')
|
|
1784
|
+
const r = installShadowJob(env)
|
|
1785
|
+
if (r.action === 'failed' || r.action === 'refused-foreign') {
|
|
1786
|
+
errOut(`shadow-install: ${r.action}${r.detail ? ` — ${r.detail}` : ''}\n plist: ${r.path}\n`)
|
|
1787
|
+
return 1
|
|
1788
|
+
}
|
|
1789
|
+
out(`shadow burn-in job ${r.action} (plist ${r.changed ? 'written' : 'unchanged'}): ${r.path}\n`)
|
|
1790
|
+
return 0
|
|
1791
|
+
}
|
|
1792
|
+
case 'shadow-uninstall': {
|
|
1793
|
+
// Stop + remove the burn-in job (bootout + rm) — the CORRECT halt (KeepAlive would
|
|
1794
|
+
// respawn past the STOP sentinel). Refuses a non-foundation plist (ownership guard).
|
|
1795
|
+
const { uninstallShadowJob } = await import('../shadow/install.ts')
|
|
1796
|
+
const r = uninstallShadowJob(env)
|
|
1797
|
+
if (r.action === 'failed' || r.action === 'refused-foreign') {
|
|
1798
|
+
errOut(`shadow-uninstall: ${r.action}${r.detail ? ` — ${r.detail}` : ''}\n plist: ${r.path}\n`)
|
|
1799
|
+
return 1
|
|
1800
|
+
}
|
|
1801
|
+
out(`shadow burn-in job ${r.action}: ${r.path}\n`)
|
|
1802
|
+
return 0
|
|
1803
|
+
}
|
|
1775
1804
|
default: {
|
|
1776
1805
|
// `iapeer <runtime>` (launch) — folder-launch the cwd's peer, ALWAYS fresh.
|
|
1777
1806
|
if (verb && isRuntime(verb)) {
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { spawnSync } from 'node:child_process'
|
|
4
|
+
import { tmpdir } from 'node:os'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { isFoundationOwnedPlist } from '../launch/launchd.ts'
|
|
7
|
+
import {
|
|
8
|
+
SHADOW_PERSONALITY,
|
|
9
|
+
SHADOW_PLIST_LABEL,
|
|
10
|
+
buildShadowPlistSpec,
|
|
11
|
+
cycleShadowJob,
|
|
12
|
+
installShadowJob,
|
|
13
|
+
installShadowPlist,
|
|
14
|
+
shadowPlistPath,
|
|
15
|
+
uninstallShadowJob,
|
|
16
|
+
} from './install.ts'
|
|
17
|
+
|
|
18
|
+
// Hermetic: every plist write lands under IAPEER_LAUNCHAGENTS_DIR / IAPEER_ROOT overrides, and
|
|
19
|
+
// the suite runs with IAPEER_TEST_SANDBOX=1 (process.env) so the launchctl-touching paths
|
|
20
|
+
// (installShadowJob / cycleShadowJob / uninstallShadowJob) fail-CLOSED to skipped-sandbox — they
|
|
21
|
+
// NEVER load/unload a real launchd job. No tmux, no @xterm (install.ts is pure plumbing).
|
|
22
|
+
|
|
23
|
+
const tmpDirs: string[] = []
|
|
24
|
+
function mkEnv(extra: Record<string, string> = {}): NodeJS.ProcessEnv {
|
|
25
|
+
const root = mkdtempSync(join(tmpdir(), 'iapeer-shadow-install-'))
|
|
26
|
+
tmpDirs.push(root)
|
|
27
|
+
return {
|
|
28
|
+
IAPEER_LAUNCHAGENTS_DIR: join(root, 'LaunchAgents'),
|
|
29
|
+
IAPEER_ROOT: join(root, 'iapeer'),
|
|
30
|
+
HOME: root,
|
|
31
|
+
PATH: '/usr/bin:/bin',
|
|
32
|
+
...extra,
|
|
33
|
+
} as NodeJS.ProcessEnv
|
|
34
|
+
}
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
while (tmpDirs.length) rmSync(tmpDirs.pop()!, { recursive: true, force: true })
|
|
37
|
+
})
|
|
38
|
+
const plutilLint = (path: string): boolean => spawnSync('plutil', ['-lint', path], { encoding: 'utf8' }).status === 0
|
|
39
|
+
|
|
40
|
+
describe('buildShadowPlistSpec', () => {
|
|
41
|
+
test('label is com.iapeer.shadow-fidelity and runs the INSTALLED `iapeer shadow`', () => {
|
|
42
|
+
const spec = buildShadowPlistSpec({ HOME: '/Users/x' } as NodeJS.ProcessEnv)
|
|
43
|
+
expect(spec.label).toBe(SHADOW_PLIST_LABEL)
|
|
44
|
+
expect(SHADOW_PLIST_LABEL).toBe('com.iapeer.shadow-fidelity')
|
|
45
|
+
expect(spec.programArguments).toEqual(['/Users/x/.local/bin/iapeer', 'shadow'])
|
|
46
|
+
})
|
|
47
|
+
test('launchd-minimal PATH includes /opt/homebrew/bin (tmux lives there — the observer shells read-only tmux)', () => {
|
|
48
|
+
expect(buildShadowPlistSpec({ HOME: '/Users/x' } as NodeJS.ProcessEnv).environment.PATH).toContain('/opt/homebrew/bin')
|
|
49
|
+
})
|
|
50
|
+
test('stdout/stderr land under ~/.iapeer/logs/iapeer', () => {
|
|
51
|
+
const spec = buildShadowPlistSpec(mkEnv())
|
|
52
|
+
expect(spec.stdoutPath).toContain(join('logs', 'iapeer', 'shadow-fidelity.launchd-stdout.log'))
|
|
53
|
+
expect(spec.stderrPath).toContain(join('logs', 'iapeer', 'shadow-fidelity.launchd-stderr.log'))
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
describe('installShadowPlist', () => {
|
|
58
|
+
test('writes a valid, foundation-owned (sentinel) always-on plist under the override dir', () => {
|
|
59
|
+
const env = mkEnv()
|
|
60
|
+
const r = installShadowPlist(env)
|
|
61
|
+
expect(r.path).toBe(shadowPlistPath(env))
|
|
62
|
+
expect(r.changed).toBe(true)
|
|
63
|
+
expect(existsSync(r.path)).toBe(true)
|
|
64
|
+
expect(isFoundationOwnedPlist(r.path)).toBe(true) // carries the ownership sentinel
|
|
65
|
+
expect(plutilLint(r.path)).toBe(true)
|
|
66
|
+
const xml = readFileSync(r.path, 'utf8')
|
|
67
|
+
expect(xml).toContain(`<string>${SHADOW_PLIST_LABEL}</string>`)
|
|
68
|
+
expect(xml).toContain('<string>shadow</string>')
|
|
69
|
+
expect(xml).toContain('<key>RunAtLoad</key>')
|
|
70
|
+
expect(xml).toContain('<key>KeepAlive</key>')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('idempotent by content: an unchanged re-install does NOT rewrite (changed:false, mtime stable); a changed PATH DOES', () => {
|
|
74
|
+
const env = mkEnv()
|
|
75
|
+
const first = installShadowPlist(env)
|
|
76
|
+
expect(first.changed).toBe(true)
|
|
77
|
+
const mtime1 = statSync(first.path).mtimeMs
|
|
78
|
+
const second = installShadowPlist(env)
|
|
79
|
+
expect(second.changed).toBe(false)
|
|
80
|
+
expect(statSync(second.path).mtimeMs).toBe(mtime1) // file untouched (no BTM spam)
|
|
81
|
+
// a different rendered plist (PATH override) rewrites
|
|
82
|
+
const third = installShadowPlist(mkEnv({ IAPEER_SHADOW_PATH: '/opt/homebrew/bin:/usr/bin:/bin', IAPEER_LAUNCHAGENTS_DIR: env.IAPEER_LAUNCHAGENTS_DIR!, IAPEER_ROOT: env.IAPEER_ROOT!, HOME: env.HOME! }))
|
|
83
|
+
expect(third.changed).toBe(true)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('collision guard: REFUSES to overwrite a foreign (no-sentinel) plist at the label', () => {
|
|
87
|
+
const env = mkEnv()
|
|
88
|
+
const laDir = env.IAPEER_LAUNCHAGENTS_DIR!
|
|
89
|
+
mkdirSync(laDir, { recursive: true })
|
|
90
|
+
const path = shadowPlistPath(env)
|
|
91
|
+
const foreign = '<?xml version="1.0"?>\n<plist><dict><key>Label</key><string>com.iapeer.shadow-fidelity</string></dict></plist>\n'
|
|
92
|
+
writeFileSync(path, foreign)
|
|
93
|
+
expect(() => installShadowPlist(env)).toThrow(/foundation-managed|refus/i)
|
|
94
|
+
expect(readFileSync(path, 'utf8')).toBe(foreign) // untouched — guard fires before any write
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('installShadowJob (sandbox fail-closed)', () => {
|
|
99
|
+
test('writes the plist FILE but NEVER touches launchctl under IAPEER_TEST_SANDBOX → skipped-sandbox', () => {
|
|
100
|
+
const env = mkEnv()
|
|
101
|
+
const r = installShadowJob(env)
|
|
102
|
+
expect(r.action).toBe('skipped-sandbox')
|
|
103
|
+
expect(r.path).toBe(shadowPlistPath(env))
|
|
104
|
+
expect(r.changed).toBe(true)
|
|
105
|
+
expect(existsSync(r.path)).toBe(true) // file written to the override dir
|
|
106
|
+
expect(isFoundationOwnedPlist(r.path)).toBe(true)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('cycleShadowJob (deploy re-cycle hook)', () => {
|
|
111
|
+
test('returns null when no plist exists (nothing to recycle)', () => {
|
|
112
|
+
expect(cycleShadowJob(mkEnv())).toBeNull()
|
|
113
|
+
})
|
|
114
|
+
test('a written foundation plist recycles to skipped-sandbox under the test flag (never a real launchctl cycle)', () => {
|
|
115
|
+
const env = mkEnv()
|
|
116
|
+
installShadowPlist(env)
|
|
117
|
+
expect(cycleShadowJob(env)?.state).toBe('skipped-sandbox')
|
|
118
|
+
})
|
|
119
|
+
test('a foreign plist at the label is NOT recycled (ownership guard) → null', () => {
|
|
120
|
+
const env = mkEnv()
|
|
121
|
+
mkdirSync(env.IAPEER_LAUNCHAGENTS_DIR!, { recursive: true })
|
|
122
|
+
writeFileSync(shadowPlistPath(env), '<?xml version="1.0"?>\n<plist><dict/></plist>\n')
|
|
123
|
+
expect(cycleShadowJob(env)).toBeNull()
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
describe('uninstallShadowJob', () => {
|
|
128
|
+
test('absent plist → absent (no-op)', () => {
|
|
129
|
+
expect(uninstallShadowJob(mkEnv()).action).toBe('absent')
|
|
130
|
+
})
|
|
131
|
+
test('foundation plist under sandbox → skipped-sandbox, plist left in place (no real bootout)', () => {
|
|
132
|
+
const env = mkEnv()
|
|
133
|
+
installShadowPlist(env)
|
|
134
|
+
const r = uninstallShadowJob(env)
|
|
135
|
+
expect(r.action).toBe('skipped-sandbox')
|
|
136
|
+
expect(existsSync(shadowPlistPath(env))).toBe(true)
|
|
137
|
+
})
|
|
138
|
+
test('foreign plist → refused-foreign, untouched', () => {
|
|
139
|
+
const env = mkEnv()
|
|
140
|
+
mkdirSync(env.IAPEER_LAUNCHAGENTS_DIR!, { recursive: true })
|
|
141
|
+
const path = shadowPlistPath(env)
|
|
142
|
+
writeFileSync(path, '<?xml version="1.0"?>\n<plist><dict/></plist>\n')
|
|
143
|
+
expect(uninstallShadowJob(env).action).toBe('refused-foreign')
|
|
144
|
+
expect(existsSync(path)).toBe(true)
|
|
145
|
+
})
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
describe('exports wire to the running job', () => {
|
|
149
|
+
test('SHADOW_PERSONALITY/LABEL match the live burn-in job naming', () => {
|
|
150
|
+
expect(SHADOW_PERSONALITY).toBe('shadow-fidelity')
|
|
151
|
+
expect(SHADOW_PLIST_LABEL).toBe('com.iapeer.shadow-fidelity')
|
|
152
|
+
})
|
|
153
|
+
})
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// Code-managed install / teardown / update-recycle for the read-only tmux→pty fidelity
|
|
2
|
+
// BURN-IN job (`iapeer shadow`). The OBSERVER itself is src/shadow/index.ts (it loads
|
|
3
|
+
// @xterm/headless); THIS module is PURE launchd plumbing — it imports NO @xterm — so the
|
|
4
|
+
// deploy path (update/rollback) and the CLI can install/recycle the job without pulling the
|
|
5
|
+
// terminal emulator into their import graph (the daemon hot path stays @xterm-free).
|
|
6
|
+
//
|
|
7
|
+
// The job is a foundation-OWNED, always-on launchd LaunchAgent (com.iapeer.shadow-fidelity)
|
|
8
|
+
// running the INSTALLED binary `iapeer shadow`. It is NOT a registry peer — it receives no
|
|
9
|
+
// IAP and has no peer profile — so recycleFoundationOwnedInfraJobs' registry SCAN cannot see
|
|
10
|
+
// it; cycleShadowJob recycles it by LABEL instead. It carries the same ownership sentinel as
|
|
11
|
+
// every foundation plist, so the collision guard (install) and the recycle guard both
|
|
12
|
+
// recognize it as ours. KeepAlive owns its lifecycle; stop it with `iapeer shadow-uninstall`
|
|
13
|
+
// (bootout + rm), NEVER the STOP sentinel file (KeepAlive would just respawn straight past it).
|
|
14
|
+
|
|
15
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
16
|
+
import { homedir } from 'node:os'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
import { spawnSync } from 'node:child_process'
|
|
19
|
+
import { IAPEER_DIR } from '../core/constants.ts'
|
|
20
|
+
import { IapError } from '../core/errors.ts'
|
|
21
|
+
import { iapeerBinPath } from '../install/index.ts'
|
|
22
|
+
import { pluginLogsDir } from '../storage/index.ts'
|
|
23
|
+
import {
|
|
24
|
+
cycleLaunchdJob,
|
|
25
|
+
isFoundationOwnedPlist,
|
|
26
|
+
launchAgentsDir,
|
|
27
|
+
launchctlBootstrap,
|
|
28
|
+
launchdLabel,
|
|
29
|
+
renderLaunchdPlist,
|
|
30
|
+
type LaunchdJobCycleResult,
|
|
31
|
+
type LaunchdPlistSpec,
|
|
32
|
+
} from '../launch/launchd.ts'
|
|
33
|
+
|
|
34
|
+
/** The burn-in job's personality stem → label com.iapeer.shadow-fidelity (the running job). */
|
|
35
|
+
export const SHADOW_PERSONALITY = 'shadow-fidelity'
|
|
36
|
+
/** com.iapeer.shadow-fidelity — keyed through launchdLabel so the scheme has ONE source. */
|
|
37
|
+
export const SHADOW_PLIST_LABEL = launchdLabel(SHADOW_PERSONALITY)
|
|
38
|
+
|
|
39
|
+
export function shadowPlistPath(env: NodeJS.ProcessEnv = process.env): string {
|
|
40
|
+
return join(launchAgentsDir(env), `${SHADOW_PLIST_LABEL}.plist`)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The STOP sentinel the observer polls (under <eventLogDir>=~/.iapeer/logs/iapeer). Under
|
|
44
|
+
* KeepAlive a STALE one would respawn the observer straight into an immediate exit → a
|
|
45
|
+
* throttle-storm, so installShadowJob clears it before (re)loading. */
|
|
46
|
+
function shadowStopFile(env: NodeJS.ProcessEnv = process.env): string {
|
|
47
|
+
return join(pluginLogsDir('iapeer', { env }), 'shadow-fidelity.STOP')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function inSandbox(env: NodeJS.ProcessEnv): boolean {
|
|
51
|
+
return env.IAPEER_TEST_SANDBOX === '1' || process.env.IAPEER_TEST_SANDBOX === '1'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Resolve the gui-domain uid, or null when `id -u` is non-numeric (never assume 0). */
|
|
55
|
+
function uidOrNull(): string | null {
|
|
56
|
+
const u = (spawnSync('id', ['-u'], { encoding: 'utf8' }).stdout ?? '').trim()
|
|
57
|
+
return /^\d+$/.test(u) ? u : null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Is com.iapeer.shadow-fidelity currently loaded in the gui domain? */
|
|
61
|
+
function isShadowLoaded(): boolean {
|
|
62
|
+
const uid = uidOrNull()
|
|
63
|
+
if (!uid) return false
|
|
64
|
+
return spawnSync('launchctl', ['print', `gui/${uid}/${SHADOW_PLIST_LABEL}`], { stdio: 'ignore' }).status === 0
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Build the burn-in job's launchd plist spec (PURE — render/lint-testable). Runs the
|
|
69
|
+
* INSTALLED `iapeer shadow` (prod decoupled from the src tree). The launchd-minimal PATH
|
|
70
|
+
* MUST include /opt/homebrew/bin: the observer shells READ-ONLY tmux (capture-pane /
|
|
71
|
+
* has-session / display), and tmux lives there — a bare PATH would make every tmux probe
|
|
72
|
+
* fail (no peers enumerated). Deterministic (curated PATH, not env.PATH) so the rendered
|
|
73
|
+
* plist is byte-stable across deploys → installShadowPlist's write-if-changed stays a no-op
|
|
74
|
+
* when nothing actually changed (no Background Task Management notification spam).
|
|
75
|
+
*/
|
|
76
|
+
export function buildShadowPlistSpec(env: NodeJS.ProcessEnv = process.env): LaunchdPlistSpec {
|
|
77
|
+
const home = env.HOME?.trim() || homedir()
|
|
78
|
+
const defaultPath = `${home}/.local/bin:${home}/.bun/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin`
|
|
79
|
+
const logDir = pluginLogsDir('iapeer', { env })
|
|
80
|
+
const environment: Record<string, string> = { PATH: env.IAPEER_SHADOW_PATH?.trim() || defaultPath }
|
|
81
|
+
// Sandbox/test parity (mirror of installDaemonPlist / installAlwaysOnPlist): bake the
|
|
82
|
+
// non-default root overrides so a SANDBOXED install resolves the SAME isolated tree the
|
|
83
|
+
// observer reads. In production these are unset → nothing baked, real ~/.iapeer + /tmp used.
|
|
84
|
+
for (const key of ['IAPEER_ROOT', 'IAPEER_SOCK_DIR', 'IAPEER_LAUNCHAGENTS_DIR'] as const) {
|
|
85
|
+
if (env[key]?.trim()) environment[key] = env[key]!.trim()
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
label: SHADOW_PLIST_LABEL,
|
|
89
|
+
programArguments: [iapeerBinPath(env), 'shadow'],
|
|
90
|
+
workingDirectory: home, // cwd-agnostic (the observer resolves paths from IAPEER_ROOT/cfg)
|
|
91
|
+
environment,
|
|
92
|
+
stdoutPath: join(logDir, 'shadow-fidelity.launchd-stdout.log'),
|
|
93
|
+
stderrPath: join(logDir, 'shadow-fidelity.launchd-stderr.log'),
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface InstallShadowPlistResult {
|
|
98
|
+
path: string
|
|
99
|
+
/** false when the on-disk plist already matched byte-for-byte → NO write (no BTM spam). */
|
|
100
|
+
changed: boolean
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Write the burn-in plist (file only — does NOT load it). IDEMPOTENT BY CONTENT
|
|
105
|
+
* (write-if-changed, mirror of installDaemonPlist: a byte-identical rewrite trips the owner's
|
|
106
|
+
* Background Task Management notification for zero benefit). Collision guard: refuses to
|
|
107
|
+
* overwrite a com.iapeer.shadow-fidelity.plist that lacks the ownership sentinel (a foreign
|
|
108
|
+
* manager owns it) — the guard fires FIRST, before any mkdir/write, so a refusal leaves the
|
|
109
|
+
* filesystem untouched.
|
|
110
|
+
*/
|
|
111
|
+
export function installShadowPlist(env: NodeJS.ProcessEnv = process.env): InstallShadowPlistResult {
|
|
112
|
+
const path = shadowPlistPath(env)
|
|
113
|
+
if (existsSync(path) && !isFoundationOwnedPlist(path)) {
|
|
114
|
+
throw new IapError(
|
|
115
|
+
`refusing to overwrite ${path}: ${SHADOW_PLIST_LABEL} exists but is not foundation-managed ` +
|
|
116
|
+
`(no ownership sentinel) — another manager owns it`,
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
const rendered = renderLaunchdPlist(buildShadowPlistSpec(env))
|
|
120
|
+
mkdirSync(launchAgentsDir(env), { recursive: true })
|
|
121
|
+
// Never mkdir the REAL ~/.iapeer log dir under a sandbox that forgot IAPEER_ROOT (mirror
|
|
122
|
+
// installAlwaysOnPlist's guard); a real run (no sandbox flag) always makes it.
|
|
123
|
+
const logDir = pluginLogsDir('iapeer', { env })
|
|
124
|
+
const realRoot = join(homedir(), IAPEER_DIR)
|
|
125
|
+
if (!(inSandbox(env) && logDir.startsWith(`${realRoot}/`))) {
|
|
126
|
+
mkdirSync(logDir, { recursive: true, mode: 0o700 })
|
|
127
|
+
}
|
|
128
|
+
let existing: string | null = null
|
|
129
|
+
try {
|
|
130
|
+
existing = readFileSync(path, 'utf8')
|
|
131
|
+
} catch {
|
|
132
|
+
existing = null
|
|
133
|
+
}
|
|
134
|
+
if (existing === rendered) return { path, changed: false }
|
|
135
|
+
writeFileSync(path, rendered, { mode: 0o644 })
|
|
136
|
+
return { path, changed: true }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export type ShadowJobAction =
|
|
140
|
+
| 'loaded' // bootstrapped now (was not loaded)
|
|
141
|
+
| 'reloaded' // plist changed on an already-loaded job → bootout+bootstrap applied the new one
|
|
142
|
+
| 'already-loaded' // loaded and the plist did not change → left running untouched
|
|
143
|
+
| 'refused-foreign' // a non-foundation plist sits at the label → never loaded
|
|
144
|
+
| 'failed' // launchctl bootstrap/cycle failed (detail carries the rescue recipe)
|
|
145
|
+
| 'skipped-sandbox' // IAPEER_TEST_SANDBOX=1 → never touch real launchd
|
|
146
|
+
|
|
147
|
+
export interface InstallShadowJobResult extends InstallShadowPlistResult {
|
|
148
|
+
action: ShadowJobAction
|
|
149
|
+
detail?: string
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Install the burn-in plist AND ensure the job is live: write-if-changed, clear a stale STOP
|
|
154
|
+
* sentinel, then load it (or RELOAD via bootout+bootstrap when the plist content changed on
|
|
155
|
+
* an already-loaded job — bootstrap alone is idempotent and would NOT pick up a changed
|
|
156
|
+
* plist). Idempotent: a repeat call on an unchanged, loaded job is a no-op ('already-loaded').
|
|
157
|
+
* Sandbox-safe: under IAPEER_TEST_SANDBOX it writes the (overridden-dir) plist FILE but never
|
|
158
|
+
* calls launchctl ('skipped-sandbox'). This is the code-managed replacement for a hand-written
|
|
159
|
+
* burn-in plist — reproducible, ownership-sentinel-guarded, fleet-deployable.
|
|
160
|
+
*/
|
|
161
|
+
export function installShadowJob(env: NodeJS.ProcessEnv = process.env): InstallShadowJobResult {
|
|
162
|
+
const { path, changed } = installShadowPlist(env)
|
|
163
|
+
if (inSandbox(env)) return { path, changed, action: 'skipped-sandbox' }
|
|
164
|
+
// Clear a STALE STOP sentinel BEFORE (re)loading — else KeepAlive respawns the observer
|
|
165
|
+
// into an instant exit (it polls the STOP file) → 10s-throttled respawn storm.
|
|
166
|
+
try {
|
|
167
|
+
unlinkSync(shadowStopFile(env))
|
|
168
|
+
} catch {
|
|
169
|
+
/* none present */
|
|
170
|
+
}
|
|
171
|
+
if (isShadowLoaded()) {
|
|
172
|
+
if (!changed) return { path, changed, action: 'already-loaded' }
|
|
173
|
+
const c = cycleLaunchdJob(SHADOW_PLIST_LABEL, path, env)
|
|
174
|
+
return c.state === 'restarted'
|
|
175
|
+
? { path, changed, action: 'reloaded' }
|
|
176
|
+
: { path, changed, action: 'failed', detail: c.detail }
|
|
177
|
+
}
|
|
178
|
+
const b = launchctlBootstrap(SHADOW_PERSONALITY, path, env)
|
|
179
|
+
const action: ShadowJobAction =
|
|
180
|
+
b.state === 'loaded'
|
|
181
|
+
? 'loaded'
|
|
182
|
+
: b.state === 'already-loaded'
|
|
183
|
+
? 'already-loaded'
|
|
184
|
+
: b.state === 'refused-foreign'
|
|
185
|
+
? 'refused-foreign'
|
|
186
|
+
: b.state === 'skipped-sandbox'
|
|
187
|
+
? 'skipped-sandbox'
|
|
188
|
+
: 'failed'
|
|
189
|
+
return { path, changed, action, detail: b.detail }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Recycle the burn-in job by LABEL after a binary swap (deploy path). The plist runs the
|
|
194
|
+
* just-replaced `iapeer shadow` off the same binary, so the SAME LWCR managed-launch-constraint
|
|
195
|
+
* bomb that bit the daemon (EX_CONFIG(78) on a KeepAlive respawn into the stale constraint)
|
|
196
|
+
* applies here too — only a full bootout→bootstrap clears it. It is NOT a registry peer, so
|
|
197
|
+
* recycleFoundationOwnedInfraJobs' registry scan misses it; this cycles it directly. Only
|
|
198
|
+
* touches a LOADED, foundation-owned job: absent / foreign / not-loaded → null (no-op).
|
|
199
|
+
* Sandbox-safe (cycleLaunchdJob returns skipped-sandbox under the test flag).
|
|
200
|
+
*/
|
|
201
|
+
export function cycleShadowJob(env: NodeJS.ProcessEnv = process.env): LaunchdJobCycleResult | null {
|
|
202
|
+
const path = shadowPlistPath(env)
|
|
203
|
+
if (!existsSync(path) || !isFoundationOwnedPlist(path)) return null
|
|
204
|
+
return cycleLaunchdJob(SHADOW_PLIST_LABEL, path, env)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export type ShadowUninstallAction =
|
|
208
|
+
| 'booted-out' // job was loaded → booted out, plist removed
|
|
209
|
+
| 'removed-not-loaded' // plist removed, job was not loaded
|
|
210
|
+
| 'absent' // nothing to do (no plist)
|
|
211
|
+
| 'refused-foreign' // the plist is not foundation-owned → left untouched
|
|
212
|
+
| 'skipped-sandbox'
|
|
213
|
+
| 'failed'
|
|
214
|
+
|
|
215
|
+
export interface UninstallShadowJobResult {
|
|
216
|
+
path: string
|
|
217
|
+
action: ShadowUninstallAction
|
|
218
|
+
detail?: string
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Stop and remove the burn-in job — the CORRECT way to halt it (the STOP sentinel would be
|
|
223
|
+
* respawned past by KeepAlive): bootout the loaded job, then delete the plist. Refuses a
|
|
224
|
+
* non-foundation plist (ownership guard). Sandbox-safe. Reversibility for the install verb.
|
|
225
|
+
*/
|
|
226
|
+
export function uninstallShadowJob(env: NodeJS.ProcessEnv = process.env): UninstallShadowJobResult {
|
|
227
|
+
const path = shadowPlistPath(env)
|
|
228
|
+
if (!existsSync(path)) return { path, action: 'absent' }
|
|
229
|
+
if (!isFoundationOwnedPlist(path)) {
|
|
230
|
+
return { path, action: 'refused-foreign', detail: `${path} is not foundation-owned — not booting it out` }
|
|
231
|
+
}
|
|
232
|
+
if (inSandbox(env)) return { path, action: 'skipped-sandbox' }
|
|
233
|
+
let action: ShadowUninstallAction = 'removed-not-loaded'
|
|
234
|
+
if (isShadowLoaded()) {
|
|
235
|
+
const uid = uidOrNull()
|
|
236
|
+
const r = uid
|
|
237
|
+
? spawnSync('launchctl', ['bootout', `gui/${uid}/${SHADOW_PLIST_LABEL}`], { encoding: 'utf8' })
|
|
238
|
+
: { status: 1, stderr: 'cannot resolve uid' }
|
|
239
|
+
if (r.status !== 0 && isShadowLoaded()) {
|
|
240
|
+
return { path, action: 'failed', detail: `launchctl bootout failed: ${(r.stderr ?? '').trim() || `exit ${r.status}`}` }
|
|
241
|
+
}
|
|
242
|
+
action = 'booted-out'
|
|
243
|
+
}
|
|
244
|
+
// Also clear any STOP sentinel so a later reinstall starts clean.
|
|
245
|
+
try {
|
|
246
|
+
unlinkSync(shadowStopFile(env))
|
|
247
|
+
} catch {
|
|
248
|
+
/* none */
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
rmSync(path)
|
|
252
|
+
} catch {
|
|
253
|
+
/* best-effort */
|
|
254
|
+
}
|
|
255
|
+
return { path, action }
|
|
256
|
+
}
|
package/src/update/index.ts
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
type DaemonRestartResult,
|
|
42
42
|
type LaunchdJobCycleResult,
|
|
43
43
|
} from '../launch/launchd.ts'
|
|
44
|
+
import { cycleShadowJob, SHADOW_PERSONALITY, SHADOW_PLIST_LABEL } from '../shadow/install.ts'
|
|
44
45
|
import { defaultDaemonSocketPath } from '../daemon/index.ts'
|
|
45
46
|
|
|
46
47
|
/** The npm package the foundation publishes / updates from. */
|
|
@@ -242,6 +243,14 @@ export function recycleFoundationOwnedInfraJobs(env: NodeJS.ProcessEnv = process
|
|
|
242
243
|
const r = cycleLaunchdJob(label, plist, env)
|
|
243
244
|
out.push({ personality: peer.personality, runtime, label, state: r.state, detail: r.detail })
|
|
244
245
|
}
|
|
246
|
+
// The tmux→pty fidelity BURN-IN job (com.iapeer.shadow-fidelity) is foundation-owned and
|
|
247
|
+
// runs the just-replaced `iapeer shadow` off the same binary, so it carries the same LWCR
|
|
248
|
+
// constraint — but it is NOT a registry peer, so the scan above cannot reach it. Cycle it
|
|
249
|
+
// by label too (no-op when absent / not-loaded / foreign).
|
|
250
|
+
const shadow = cycleShadowJob(env)
|
|
251
|
+
if (shadow) {
|
|
252
|
+
out.push({ personality: SHADOW_PERSONALITY, runtime: 'foundation', label: SHADOW_PLIST_LABEL, state: shadow.state, detail: shadow.detail })
|
|
253
|
+
}
|
|
245
254
|
return out
|
|
246
255
|
}
|
|
247
256
|
|