@sockulags/agent-os 0.11.0 → 0.12.1

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.
Files changed (43) hide show
  1. package/README.md +155 -127
  2. package/cli/index.mjs +1022 -735
  3. package/cli/index.test.mjs +574 -300
  4. package/hooks/hooks.json +17 -0
  5. package/package.json +34 -32
  6. package/policy.md +47 -45
  7. package/skills/batch-work/SKILL.md +43 -43
  8. package/skills/batch-work/agents/openai.yaml +2 -2
  9. package/skills/batch-work/references/manifest.md +100 -100
  10. package/skills/batch-work/scripts/manifest-hash.mjs +263 -263
  11. package/skills/batch-work/scripts/test-manifest-hash.mjs +132 -132
  12. package/skills/chart-work/SKILL.md +37 -37
  13. package/skills/chart-work/references/elicitation.md +25 -25
  14. package/skills/chart-work/references/map.md +84 -84
  15. package/skills/chart-work/references/prototypes.md +21 -21
  16. package/skills/check-work/SKILL.md +61 -0
  17. package/skills/deliver-work/SKILL.md +12 -12
  18. package/skills/deliver-work/workflow.md +119 -113
  19. package/skills/diagnose-before-fix/SKILL.md +24 -24
  20. package/skills/dispatch-next/SKILL.md +28 -28
  21. package/skills/init-agent-os/SKILL.md +54 -39
  22. package/skills/init-agent-os/references/repository-init.md +74 -0
  23. package/skills/init-agent-os/scripts/policy-block.ps1 +87 -87
  24. package/skills/list-skills/SKILL.md +43 -42
  25. package/skills/list-skills/agents/openai.yaml +2 -2
  26. package/skills/plain-voice/SKILL.md +39 -0
  27. package/skills/plain-voice/references/tells.md +82 -0
  28. package/skills/proportional-testing/SKILL.md +30 -30
  29. package/skills/proportional-testing/references/strategy.md +75 -75
  30. package/skills/quality-ratchet/SKILL.md +50 -0
  31. package/skills/quality-ratchet/scripts/quality-delta.mjs +752 -0
  32. package/skills/quality-ratchet/scripts/quality-delta.test.mjs +514 -0
  33. package/skills/scope-guard/SKILL.md +21 -21
  34. package/skills/shape-work/SKILL.md +39 -39
  35. package/skills/shape-work/references/implementation-issues.md +69 -69
  36. package/skills/shape-work/references/mockups.md +23 -23
  37. package/skills/simplifier/SKILL.md +34 -34
  38. package/skills/simplifier/agents/openai.yaml +2 -2
  39. package/skills/simplifier-audit/SKILL.md +28 -28
  40. package/skills/simplifier-audit/agents/openai.yaml +2 -2
  41. package/skills/simplifier-review/SKILL.md +36 -29
  42. package/skills/verify-before-done/SKILL.md +29 -29
  43. package/skills/writing-skills/SKILL.md +68 -68
@@ -1,300 +1,574 @@
1
- import assert from 'node:assert/strict'
2
- import fs from 'node:fs'
3
- import os from 'node:os'
4
- import path from 'node:path'
5
- import test, { after } from 'node:test'
6
- import {
7
- buildPlan,
8
- executeInstall,
9
- INSTALL_MANIFEST,
10
- PACKAGE_NAME,
11
- parseArgs,
12
- planPolicyUpdate,
13
- resolveOptions,
14
- run,
15
- targetSkillRoot,
16
- VERSION
17
- } from './index.mjs'
18
-
19
- const temporaryRoots = []
20
-
21
- function temporaryRoot() {
22
- const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-os-cli-'))
23
- temporaryRoots.push(root)
24
- return root
25
- }
26
-
27
- function silentOutput() {
28
- return { write() {} }
29
- }
30
-
31
- after(() => {
32
- for (const root of temporaryRoots) fs.rmSync(root, { recursive: true, force: true })
33
- })
34
-
35
- test('defaults to guided direct install', () => {
36
- assert.deepEqual(parseArgs([]), {
37
- command: 'install',
38
- platform: null,
39
- method: null,
40
- scope: null,
41
- policy: null,
42
- yes: false,
43
- dryRun: false,
44
- help: false,
45
- version: false
46
- })
47
- })
48
-
49
- test('parses non-interactive direct update flags', () => {
50
- assert.deepEqual(parseArgs([
51
- 'update',
52
- '--platform=both',
53
- '--method',
54
- 'direct',
55
- '--scope',
56
- 'project',
57
- '--no-policy',
58
- '--yes'
59
- ]), {
60
- command: 'update',
61
- platform: 'both',
62
- method: 'direct',
63
- scope: 'project',
64
- policy: false,
65
- yes: true,
66
- dryRun: false,
67
- help: false,
68
- version: false
69
- })
70
- })
71
-
72
- test('supports Claude Code as a platform alias and upgrade as update', () => {
73
- assert.deepEqual(parseArgs(['upgrade', '--platform', 'claude-code', '--yes']), {
74
- command: 'update',
75
- platform: 'claude',
76
- method: null,
77
- scope: null,
78
- policy: null,
79
- yes: true,
80
- dryRun: false,
81
- help: false,
82
- version: false
83
- })
84
- })
85
-
86
- test('prints scoped npm commands and explains the direct default', async () => {
87
- const chunks = []
88
- await run(['--help'], { output: { write: (chunk) => chunks.push(chunk) } })
89
- const help = chunks.join('')
90
- assert.match(help, /npx @sockulags\/agent-os install/)
91
- assert.match(help, /does not require Codex or Claude Code CLI/)
92
- assert.match(help, /--method <name>\s+direct \(default\) or plugin/)
93
- })
94
-
95
- test('rejects conflicting policy flags and direct local scope', async () => {
96
- assert.throws(() => parseArgs(['install', '--policy', '--no-policy']), /one of --policy and --no-policy/)
97
- await assert.rejects(
98
- resolveOptions(parseArgs([
99
- 'install', '--platform', 'codex', '--method', 'direct', '--scope', 'local', '--yes'
100
- ]), { input: { isTTY: false }, output: { isTTY: false } }),
101
- /Local scope is available only with --method plugin/
102
- )
103
- })
104
-
105
- test('requires explicit non-interactive choices', async () => {
106
- await assert.rejects(
107
- resolveOptions(parseArgs(['install']), {
108
- input: { isTTY: false },
109
- output: { isTTY: false }
110
- }),
111
- /--yes with explicit options/
112
- )
113
- })
114
-
115
- test('builds direct filesystem plans without marketplace commands', () => {
116
- const root = temporaryRoot()
117
- const plan = buildPlan({
118
- command: 'install',
119
- platform: 'both',
120
- method: 'direct',
121
- scope: 'user',
122
- policy: false
123
- }, { homeDir: root, cwd: root })
124
- assert.deepEqual(plan.map((step) => step.kind), ['install-skills', 'install-skills'])
125
- assert.deepEqual(plan.map((step) => step.command), ['filesystem', 'filesystem'])
126
- assert.equal(plan[0].args[0], path.join(root, '.claude', 'skills'))
127
- assert.equal(plan[1].args[0], path.join(root, '.codex', 'skills'))
128
- })
129
-
130
- test('maps project and user targets for both hosts', () => {
131
- const root = temporaryRoot()
132
- const project = path.join(root, 'project')
133
- assert.equal(targetSkillRoot('codex', 'user', { homeDir: root, cwd: project }), path.join(root, '.codex', 'skills'))
134
- assert.equal(targetSkillRoot('claude', 'user', { homeDir: root, cwd: project }), path.join(root, '.claude', 'skills'))
135
- assert.equal(targetSkillRoot('codex', 'project', { homeDir: root, cwd: project }), path.join(project, '.agents', 'skills'))
136
- assert.equal(targetSkillRoot('claude', 'project', { homeDir: root, cwd: project }), path.join(project, '.claude', 'skills'))
137
- })
138
-
139
- test('installs both hosts and policy without either host CLI', async () => {
140
- const root = temporaryRoot()
141
- const homeDir = path.join(root, 'home')
142
- const cwd = path.join(root, 'project')
143
- await executeInstall({
144
- command: 'install',
145
- platform: 'both',
146
- method: 'direct',
147
- scope: 'user',
148
- policy: true
149
- }, {
150
- output: silentOutput(),
151
- homeDir,
152
- cwd,
153
- exists: () => { throw new Error('host CLI lookup must not run') },
154
- execute: () => { throw new Error('external command must not run') }
155
- })
156
-
157
- for (const agent of ['.claude', '.codex']) {
158
- const skillsRoot = path.join(homeDir, agent, 'skills')
159
- assert.equal(fs.existsSync(path.join(skillsRoot, 'shape-work', 'SKILL.md')), true)
160
- const manifest = JSON.parse(fs.readFileSync(path.join(skillsRoot, INSTALL_MANIFEST), 'utf8'))
161
- assert.equal(manifest.package, PACKAGE_NAME)
162
- assert.equal(manifest.version, VERSION)
163
- assert.ok(manifest.skills.includes('shape-work'))
164
- }
165
- assert.match(fs.readFileSync(path.join(homeDir, '.claude', 'CLAUDE.md'), 'utf8'), /BEGIN AGENT OS/)
166
- assert.match(fs.readFileSync(path.join(homeDir, '.codex', 'AGENTS.md'), 'utf8'), /BEGIN AGENT OS/)
167
- })
168
-
169
- test('updates only managed skills and preserves unrelated skills', async () => {
170
- const root = temporaryRoot()
171
- const homeDir = path.join(root, 'home')
172
- const options = {
173
- command: 'install',
174
- platform: 'codex',
175
- method: 'direct',
176
- scope: 'user',
177
- policy: false
178
- }
179
- await executeInstall(options, { output: silentOutput(), homeDir, cwd: root })
180
-
181
- const skillsRoot = path.join(homeDir, '.codex', 'skills')
182
- const managedFile = path.join(skillsRoot, 'shape-work', 'SKILL.md')
183
- const foreignFile = path.join(skillsRoot, 'my-private-skill', 'SKILL.md')
184
- fs.writeFileSync(managedFile, 'stale managed content\n')
185
- fs.mkdirSync(path.dirname(foreignFile), { recursive: true })
186
- fs.writeFileSync(foreignFile, 'private content\n')
187
-
188
- await executeInstall({ ...options, command: 'update' }, { output: silentOutput(), homeDir, cwd: root })
189
- assert.notEqual(fs.readFileSync(managedFile, 'utf8'), 'stale managed content\n')
190
- assert.equal(fs.readFileSync(foreignFile, 'utf8'), 'private content\n')
191
- })
192
-
193
- test('refuses to overwrite a same-name skill without an Agent OS manifest', async () => {
194
- const root = temporaryRoot()
195
- const homeDir = path.join(root, 'home')
196
- const collision = path.join(homeDir, '.codex', 'skills', 'shape-work', 'SKILL.md')
197
- fs.mkdirSync(path.dirname(collision), { recursive: true })
198
- fs.writeFileSync(collision, 'developer-owned\n')
199
-
200
- await assert.rejects(
201
- executeInstall({
202
- command: 'install',
203
- platform: 'codex',
204
- method: 'direct',
205
- scope: 'user',
206
- policy: false
207
- }, { output: silentOutput(), homeDir, cwd: root }),
208
- /Refusing to overwrite unmanaged skills.*shape-work/
209
- )
210
- assert.equal(fs.readFileSync(collision, 'utf8'), 'developer-owned\n')
211
- assert.equal(fs.existsSync(path.join(homeDir, '.codex', 'skills', INSTALL_MANIFEST)), false)
212
- })
213
-
214
- test('policy planning preserves surrounding text and rejects malformed markers', () => {
215
- const policy = '# Policy\n'
216
- const original = 'before\n\n<!-- BEGIN AGENT OS -->\nold\n<!-- END AGENT OS -->\nafter\n'
217
- const updated = planPolicyUpdate(original, policy)
218
- assert.equal(updated.status, 'UPDATED')
219
- assert.equal(updated.content, 'before\n\n<!-- BEGIN AGENT OS -->\n# Policy\n<!-- END AGENT OS -->\nafter\n')
220
- assert.throws(
221
- () => planPolicyUpdate('<!-- BEGIN AGENT OS -->\nbroken\n', policy),
222
- /Malformed Agent OS policy markers/
223
- )
224
- })
225
-
226
- test('malformed policy markers abort before direct skill installation', async () => {
227
- const root = temporaryRoot()
228
- const homeDir = path.join(root, 'home')
229
- const target = path.join(homeDir, '.codex', 'AGENTS.md')
230
- fs.mkdirSync(path.dirname(target), { recursive: true })
231
- fs.writeFileSync(target, '<!-- BEGIN AGENT OS -->\nbroken\n')
232
-
233
- await assert.rejects(
234
- executeInstall({
235
- command: 'install',
236
- platform: 'codex',
237
- method: 'direct',
238
- scope: 'user',
239
- policy: true
240
- }, { output: silentOutput(), homeDir, cwd: root }),
241
- /Malformed Agent OS policy markers/
242
- )
243
- assert.equal(fs.existsSync(path.join(homeDir, '.codex', 'skills')), false)
244
- })
245
-
246
- test('plugin mode still requires only the selected host CLI', async () => {
247
- const calls = []
248
- await assert.rejects(
249
- executeInstall({
250
- command: 'install',
251
- platform: 'codex',
252
- method: 'plugin',
253
- scope: 'user',
254
- policy: false
255
- }, {
256
- output: silentOutput(),
257
- exists: () => false,
258
- execute: (...args) => calls.push(args)
259
- }),
260
- /Native plugin mode requires the selected host CLIs.*Codex/s
261
- )
262
- assert.deepEqual(calls, [])
263
- })
264
-
265
- test('plugin update skips Git refresh for a registered local Codex marketplace', async () => {
266
- const calls = []
267
- await executeInstall({
268
- command: 'update',
269
- platform: 'codex',
270
- method: 'plugin',
271
- scope: 'user',
272
- policy: false
273
- }, {
274
- output: silentOutput(),
275
- exists: () => true,
276
- execute: (command, args) => {
277
- calls.push([command, args])
278
- if (args.join(' ') === 'plugin marketplace list --json') {
279
- return { status: 0, stdout: JSON.stringify({ marketplaces: [{ name: 'agent-os' }] }), stderr: '' }
280
- }
281
- if (args.join(' ') === 'plugin list --json') {
282
- return {
283
- status: 0,
284
- stdout: JSON.stringify({ installed: [{
285
- marketplaceName: 'agent-os',
286
- marketplaceSource: { sourceType: 'local' }
287
- }] }),
288
- stderr: ''
289
- }
290
- }
291
- return { status: 0, stdout: '', stderr: '' }
292
- }
293
- })
294
-
295
- assert.deepEqual(calls.map((call) => call[1]), [
296
- ['plugin', 'marketplace', 'list', '--json'],
297
- ['plugin', 'list', '--json'],
298
- ['plugin', 'add', 'agent-os@agent-os']
299
- ])
300
- })
1
+ import assert from 'node:assert/strict'
2
+ import fs from 'node:fs'
3
+ import os from 'node:os'
4
+ import path from 'node:path'
5
+ import test, { after } from 'node:test'
6
+ import { fileURLToPath } from 'node:url'
7
+ import {
8
+ buildManagedHookCommand,
9
+ buildPlan,
10
+ executeInstall,
11
+ hookConfigPath,
12
+ INSTALL_MANIFEST,
13
+ PACKAGE_NAME,
14
+ parseArgs,
15
+ planManagedHookConfig,
16
+ planPolicyUpdate,
17
+ resolveOptions,
18
+ run,
19
+ targetSkillRoot,
20
+ VERSION
21
+ } from './index.mjs'
22
+
23
+ const temporaryRoots = []
24
+
25
+ function temporaryRoot() {
26
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-os-cli-'))
27
+ temporaryRoots.push(root)
28
+ return root
29
+ }
30
+
31
+ function silentOutput() {
32
+ return { write() {} }
33
+ }
34
+
35
+ function packageRootForTest() {
36
+ return path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
37
+ }
38
+
39
+ after(() => {
40
+ for (const root of temporaryRoots) fs.rmSync(root, { recursive: true, force: true })
41
+ })
42
+
43
+ test('defaults to guided direct install', () => {
44
+ assert.deepEqual(parseArgs([]), {
45
+ command: 'install',
46
+ platform: null,
47
+ method: null,
48
+ scope: null,
49
+ policy: null,
50
+ yes: false,
51
+ dryRun: false,
52
+ help: false,
53
+ version: false
54
+ })
55
+ })
56
+
57
+ test('parses non-interactive direct update flags', () => {
58
+ assert.deepEqual(parseArgs([
59
+ 'update',
60
+ '--platform=both',
61
+ '--method',
62
+ 'direct',
63
+ '--scope',
64
+ 'project',
65
+ '--no-policy',
66
+ '--yes'
67
+ ]), {
68
+ command: 'update',
69
+ platform: 'both',
70
+ method: 'direct',
71
+ scope: 'project',
72
+ policy: false,
73
+ yes: true,
74
+ dryRun: false,
75
+ help: false,
76
+ version: false
77
+ })
78
+ })
79
+
80
+ test('supports Claude Code as a platform alias and upgrade as update', () => {
81
+ assert.deepEqual(parseArgs(['upgrade', '--platform', 'claude-code', '--yes']), {
82
+ command: 'update',
83
+ platform: 'claude',
84
+ method: null,
85
+ scope: null,
86
+ policy: null,
87
+ yes: true,
88
+ dryRun: false,
89
+ help: false,
90
+ version: false
91
+ })
92
+ })
93
+
94
+ test('prints scoped npm commands and explains the direct default', async () => {
95
+ const chunks = []
96
+ await run(['--help'], { output: { write: (chunk) => chunks.push(chunk) } })
97
+ const help = chunks.join('')
98
+ assert.match(help, /npx @sockulags\/agent-os install/)
99
+ assert.match(help, /does not require Codex or Claude Code CLI/)
100
+ assert.match(help, /--method <name>\s+direct \(default\) or plugin/)
101
+ })
102
+
103
+ test('rejects conflicting policy flags and direct local scope', async () => {
104
+ assert.throws(() => parseArgs(['install', '--policy', '--no-policy']), /one of --policy and --no-policy/)
105
+ await assert.rejects(
106
+ resolveOptions(parseArgs([
107
+ 'install', '--platform', 'codex', '--method', 'direct', '--scope', 'local', '--yes'
108
+ ]), { input: { isTTY: false }, output: { isTTY: false } }),
109
+ /Local scope is available only with --method plugin/
110
+ )
111
+ })
112
+
113
+ test('requires explicit non-interactive choices', async () => {
114
+ await assert.rejects(
115
+ resolveOptions(parseArgs(['install']), {
116
+ input: { isTTY: false },
117
+ output: { isTTY: false }
118
+ }),
119
+ /--yes with explicit options/
120
+ )
121
+ })
122
+
123
+ test('builds direct filesystem plans without marketplace commands', () => {
124
+ const root = temporaryRoot()
125
+ const plan = buildPlan({
126
+ command: 'install',
127
+ platform: 'both',
128
+ method: 'direct',
129
+ scope: 'user',
130
+ policy: false
131
+ }, { homeDir: root, cwd: root })
132
+ assert.deepEqual(plan.map((step) => step.kind), ['install-skills', 'install-hooks', 'install-skills', 'install-hooks'])
133
+ assert.deepEqual(plan.map((step) => step.command), ['filesystem', 'filesystem', 'filesystem', 'filesystem'])
134
+ assert.equal(plan[0].args[0], path.join(root, '.claude', 'skills'))
135
+ assert.equal(plan[1].args[0], path.join(root, '.claude', 'settings.json'))
136
+ assert.equal(plan[2].args[0], path.join(root, '.codex', 'skills'))
137
+ assert.equal(plan[3].args[0], path.join(root, '.codex', 'hooks.json'))
138
+ })
139
+
140
+ test('maps project and user targets for both hosts', () => {
141
+ const root = temporaryRoot()
142
+ const project = path.join(root, 'project')
143
+ assert.equal(targetSkillRoot('codex', 'user', { homeDir: root, cwd: project }), path.join(root, '.codex', 'skills'))
144
+ assert.equal(targetSkillRoot('claude', 'user', { homeDir: root, cwd: project }), path.join(root, '.claude', 'skills'))
145
+ assert.equal(targetSkillRoot('codex', 'project', { homeDir: root, cwd: project }), path.join(project, '.agents', 'skills'))
146
+ assert.equal(targetSkillRoot('claude', 'project', { homeDir: root, cwd: project }), path.join(project, '.claude', 'skills'))
147
+ })
148
+
149
+ test('installs both hosts and policy without either host CLI', async () => {
150
+ const root = temporaryRoot()
151
+ const homeDir = path.join(root, 'home')
152
+ const cwd = path.join(root, 'project')
153
+ await executeInstall({
154
+ command: 'install',
155
+ platform: 'both',
156
+ method: 'direct',
157
+ scope: 'user',
158
+ policy: true
159
+ }, {
160
+ output: silentOutput(),
161
+ homeDir,
162
+ cwd,
163
+ exists: () => { throw new Error('host CLI lookup must not run') },
164
+ execute: () => { throw new Error('external command must not run') }
165
+ })
166
+
167
+ for (const agent of ['.claude', '.codex']) {
168
+ const skillsRoot = path.join(homeDir, agent, 'skills')
169
+ assert.equal(fs.existsSync(path.join(skillsRoot, 'shape-work', 'SKILL.md')), true)
170
+ const manifest = JSON.parse(fs.readFileSync(path.join(skillsRoot, INSTALL_MANIFEST), 'utf8'))
171
+ assert.equal(manifest.package, PACKAGE_NAME)
172
+ assert.equal(manifest.version, VERSION)
173
+ assert.equal(manifest.schemaVersion, 2)
174
+ assert.ok(manifest.skills.includes('shape-work'))
175
+ assert.equal(manifest.integrations.hooks.event, 'Stop')
176
+ const hookFile = path.join(homeDir, agent, agent === '.claude' ? 'settings.json' : 'hooks.json')
177
+ assert.equal(fs.existsSync(hookFile), true)
178
+ const hookConfig = JSON.parse(fs.readFileSync(hookFile, 'utf8'))
179
+ const command = hookConfig.hooks.Stop.find((group) =>
180
+ JSON.stringify(group).includes('--agent-os-hook=quality-ratchet')).hooks[0].command
181
+ assert.equal(command.includes(path.join(skillsRoot, 'quality-ratchet', 'scripts', 'quality-delta.mjs')), true)
182
+ assert.equal(command.includes(packageRootForTest()), false)
183
+ }
184
+ assert.match(fs.readFileSync(path.join(homeDir, '.claude', 'CLAUDE.md'), 'utf8'), /BEGIN AGENT OS/)
185
+ assert.match(fs.readFileSync(path.join(homeDir, '.codex', 'AGENTS.md'), 'utf8'), /BEGIN AGENT OS/)
186
+ })
187
+
188
+ test('maps direct hook configuration paths for user and project scopes', () => {
189
+ const root = temporaryRoot()
190
+ const project = path.join(root, 'project')
191
+ assert.equal(hookConfigPath('claude', 'user', { homeDir: root, cwd: project }), path.join(root, '.claude', 'settings.json'))
192
+ assert.equal(hookConfigPath('codex', 'user', { homeDir: root, cwd: project }), path.join(root, '.codex', 'hooks.json'))
193
+ assert.equal(hookConfigPath('claude', 'project', { homeDir: root, cwd: project }), path.join(project, '.claude', 'settings.json'))
194
+ assert.equal(hookConfigPath('codex', 'project', { homeDir: root, cwd: project }), path.join(project, '.codex', 'hooks.json'))
195
+ })
196
+
197
+ test('direct project install configures both hosts without replacing unrelated hooks', async () => {
198
+ const root = temporaryRoot()
199
+ const project = path.join(root, 'project')
200
+ const claudeConfig = path.join(project, '.claude', 'settings.json')
201
+ const codexConfig = path.join(project, '.codex', 'hooks.json')
202
+ fs.mkdirSync(path.dirname(claudeConfig), { recursive: true })
203
+ fs.mkdirSync(path.dirname(codexConfig), { recursive: true })
204
+ const unrelated = {
205
+ permissions: { allow: ['Read'] },
206
+ hooks: {
207
+ PreToolUse: [{ hooks: [{ type: 'command', command: 'developer-preflight' }] }],
208
+ Stop: [{ hooks: [{ type: 'command', command: 'developer-stop' }] }]
209
+ }
210
+ }
211
+ fs.writeFileSync(claudeConfig, JSON.stringify(unrelated, null, 2) + '\n')
212
+ fs.writeFileSync(codexConfig, JSON.stringify(unrelated, null, 2) + '\n')
213
+ await executeInstall({
214
+ command: 'install', platform: 'both', method: 'direct', scope: 'project', policy: false
215
+ }, { output: silentOutput(), homeDir: root, cwd: project })
216
+
217
+ for (const [file, skillsRoot] of [
218
+ [claudeConfig, path.join(project, '.claude', 'skills')],
219
+ [codexConfig, path.join(project, '.agents', 'skills')]
220
+ ]) {
221
+ const config = JSON.parse(fs.readFileSync(file, 'utf8'))
222
+ assert.deepEqual(config.permissions, unrelated.permissions)
223
+ assert.deepEqual(config.hooks.PreToolUse, unrelated.hooks.PreToolUse)
224
+ assert.deepEqual(config.hooks.Stop[0], unrelated.hooks.Stop[0])
225
+ assert.equal(config.hooks.Stop.filter((group) => JSON.stringify(group).includes('--agent-os-hook=quality-ratchet')).length, 1)
226
+ const command = config.hooks.Stop.find((group) =>
227
+ JSON.stringify(group).includes('--agent-os-hook=quality-ratchet')).hooks[0].command
228
+ assert.equal(command.includes(path.join(skillsRoot, 'quality-ratchet', 'scripts', 'quality-delta.mjs')), true)
229
+ assert.equal(command.includes(packageRootForTest()), false)
230
+ }
231
+ })
232
+
233
+ test('managed hook updates replace only the exact Agent OS entry', () => {
234
+ const durableScript = path.join(temporaryRoot(), '.codex', 'skills', 'quality-ratchet', 'scripts', 'quality-delta.mjs')
235
+ const managedCommand = buildManagedHookCommand(durableScript)
236
+ const oldCommand = '"old-node" "old-quality-delta.mjs" hook --agent-os-hook=quality-ratchet'
237
+ const original = JSON.stringify({
238
+ unrelated: true,
239
+ hooks: {
240
+ Stop: [
241
+ { hooks: [{ type: 'command', command: 'keep-me' }] },
242
+ { hooks: [{ type: 'command', command: oldCommand, timeout: 1 }] }
243
+ ]
244
+ }
245
+ }, null, 2) + '\n'
246
+ const planned = planManagedHookConfig(original, managedCommand, 'test hooks.json')
247
+ assert.equal(planned.status, 'UPDATED')
248
+ const config = JSON.parse(planned.content)
249
+ assert.equal(config.unrelated, true)
250
+ assert.equal(config.hooks.Stop[0].hooks[0].command, 'keep-me')
251
+ assert.equal(config.hooks.Stop[1].hooks[0].command, managedCommand)
252
+ assert.equal(config.hooks.Stop[1].hooks[0].timeout, 10)
253
+ assert.equal(config.hooks.Stop[1].hooks[0].command.includes(durableScript), true)
254
+ })
255
+
256
+ test('malformed or ambiguous managed hook config aborts before direct skill mutation', async () => {
257
+ const root = temporaryRoot()
258
+ const homeDir = path.join(root, 'home')
259
+ const hookFile = path.join(homeDir, '.codex', 'hooks.json')
260
+ fs.mkdirSync(path.dirname(hookFile), { recursive: true })
261
+ fs.writeFileSync(hookFile, '{ malformed')
262
+ await assert.rejects(
263
+ executeInstall({ command: 'install', platform: 'codex', method: 'direct', scope: 'user', policy: false }, {
264
+ output: silentOutput(), homeDir, cwd: root
265
+ }),
266
+ /Cannot read.*hooks.json.*no files were changed/
267
+ )
268
+ assert.equal(fs.existsSync(path.join(homeDir, '.codex', 'skills')), false)
269
+
270
+ fs.writeFileSync(hookFile, JSON.stringify({ hooks: { Stop: [
271
+ { hooks: [{ type: 'command', command: 'one --agent-os-hook=quality-ratchet' }] },
272
+ { hooks: [{ type: 'command', command: 'two --agent-os-hook=quality-ratchet' }] }
273
+ ] } }))
274
+ await assert.rejects(
275
+ executeInstall({ command: 'install', platform: 'codex', method: 'direct', scope: 'user', policy: false }, {
276
+ output: silentOutput(), homeDir, cwd: root
277
+ }),
278
+ /multiple Agent OS hook entries.*no files were changed/
279
+ )
280
+ assert.equal(fs.existsSync(path.join(homeDir, '.codex', 'skills')), false)
281
+ })
282
+
283
+ test('hook write failure restores both direct skill installs and preserves host configs byte-for-byte', async () => {
284
+ const root = temporaryRoot()
285
+ const homeDir = path.join(root, 'home')
286
+ const claudeConfig = path.join(homeDir, '.claude', 'settings.json')
287
+ const codexConfig = path.join(homeDir, '.codex', 'hooks.json')
288
+ const claudeText = '{\n "theme": "dark",\n "hooks": { "Stop": [] }\n}\n'
289
+ const codexText = '{\n "custom": true,\n "hooks": { "Stop": [] }\n}\n'
290
+ for (const [file, content] of [[claudeConfig, claudeText], [codexConfig, codexText]]) {
291
+ fs.mkdirSync(path.dirname(file), { recursive: true })
292
+ fs.writeFileSync(file, content)
293
+ }
294
+
295
+ const skillRoots = [
296
+ path.join(homeDir, '.claude', 'skills'),
297
+ path.join(homeDir, '.codex', 'skills')
298
+ ]
299
+ for (const skillsRoot of skillRoots) {
300
+ const privateFile = path.join(skillsRoot, 'private-skill', 'SKILL.md')
301
+ fs.mkdirSync(path.dirname(privateFile), { recursive: true })
302
+ fs.writeFileSync(privateFile, 'developer-owned\n')
303
+ }
304
+
305
+ const failingFs = Object.create(fs)
306
+ let injected = false
307
+ failingFs.writeFileSync = (target, ...args) => {
308
+ const isCodexHookTemporary = path.dirname(target) === path.dirname(codexConfig) &&
309
+ path.basename(target).startsWith('.hooks.json.agent-os-')
310
+ if (!injected && isCodexHookTemporary) {
311
+ injected = true
312
+ throw new Error('injected hook write failure')
313
+ }
314
+ return fs.writeFileSync(target, ...args)
315
+ }
316
+
317
+ await assert.rejects(
318
+ executeInstall({ command: 'install', platform: 'both', method: 'direct', scope: 'user', policy: false }, {
319
+ output: silentOutput(), homeDir, cwd: root, fsImpl: failingFs
320
+ }),
321
+ /injected hook write failure/
322
+ )
323
+
324
+ assert.equal(injected, true)
325
+ assert.equal(fs.readFileSync(claudeConfig, 'utf8'), claudeText)
326
+ assert.equal(fs.readFileSync(codexConfig, 'utf8'), codexText)
327
+ for (const skillsRoot of skillRoots) {
328
+ assert.equal(fs.readFileSync(path.join(skillsRoot, 'private-skill', 'SKILL.md'), 'utf8'), 'developer-owned\n')
329
+ assert.equal(fs.existsSync(path.join(skillsRoot, INSTALL_MANIFEST)), false)
330
+ assert.equal(fs.existsSync(path.join(skillsRoot, 'shape-work')), false)
331
+ assert.equal(fs.existsSync(path.join(skillsRoot, 'quality-ratchet')), false)
332
+ assert.deepEqual(fs.readdirSync(skillsRoot).filter((name) => name.startsWith('.agent-os-transaction-')), [])
333
+ }
334
+ for (const file of [claudeConfig, codexConfig]) {
335
+ assert.deepEqual(fs.readdirSync(path.dirname(file)).filter((name) => name.endsWith('.tmp')), [])
336
+ }
337
+ })
338
+
339
+ test('post-commit backup cleanup failure never rolls back installed skills or hooks', async () => {
340
+ const root = temporaryRoot()
341
+ const homeDir = path.join(root, 'home')
342
+ const options = {
343
+ command: 'install', platform: 'both', method: 'direct', scope: 'user', policy: false
344
+ }
345
+ await executeInstall(options, { output: silentOutput(), homeDir, cwd: root })
346
+
347
+ const skillRoots = [
348
+ path.join(homeDir, '.claude', 'skills'),
349
+ path.join(homeDir, '.codex', 'skills')
350
+ ]
351
+ const failingFs = Object.create(fs)
352
+ let firstBackupRemoved = false
353
+ let injected = false
354
+ let rollbackAttempted = false
355
+ failingFs.rmSync = (target, rmOptions) => {
356
+ const isTransactionBackup = path.basename(target).startsWith('.agent-os-transaction-')
357
+ if (isTransactionBackup && path.dirname(target) === skillRoots[0]) {
358
+ firstBackupRemoved = true
359
+ } else if (!injected && firstBackupRemoved && isTransactionBackup &&
360
+ path.dirname(target) === skillRoots[1]) {
361
+ injected = true
362
+ throw new Error('injected post-commit cleanup failure')
363
+ } else if (injected && skillRoots.some((skillsRoot) => path.dirname(target) === skillsRoot)) {
364
+ rollbackAttempted = true
365
+ }
366
+ return fs.rmSync(target, rmOptions)
367
+ }
368
+
369
+ await assert.rejects(
370
+ executeInstall({ ...options, command: 'update' }, {
371
+ output: silentOutput(), homeDir, cwd: root, fsImpl: failingFs
372
+ }),
373
+ /direct install committed, but failed to clean transaction backups: injected post-commit cleanup failure.*not rolled back/
374
+ )
375
+
376
+ assert.equal(firstBackupRemoved, true)
377
+ assert.equal(injected, true)
378
+ assert.equal(rollbackAttempted, false)
379
+ for (const [index, skillsRoot] of skillRoots.entries()) {
380
+ const manifest = JSON.parse(fs.readFileSync(path.join(skillsRoot, INSTALL_MANIFEST), 'utf8'))
381
+ assert.equal(manifest.schemaVersion, 2)
382
+ assert.equal(manifest.skills.includes('quality-ratchet'), true)
383
+ for (const name of manifest.skills) assert.equal(fs.existsSync(path.join(skillsRoot, name)), true)
384
+
385
+ const hookFile = index === 0
386
+ ? path.join(homeDir, '.claude', 'settings.json')
387
+ : path.join(homeDir, '.codex', 'hooks.json')
388
+ const hookConfig = JSON.parse(fs.readFileSync(hookFile, 'utf8'))
389
+ const managedHooks = hookConfig.hooks.Stop.filter((group) =>
390
+ JSON.stringify(group).includes('--agent-os-hook=quality-ratchet'))
391
+ assert.equal(managedHooks.length, 1)
392
+ assert.equal(managedHooks[0].hooks[0].command.includes(
393
+ path.join(skillsRoot, 'quality-ratchet', 'scripts', 'quality-delta.mjs')), true)
394
+ }
395
+ assert.deepEqual(fs.readdirSync(skillRoots[0]).filter((name) =>
396
+ name.startsWith('.agent-os-transaction-')), [])
397
+ assert.equal(fs.readdirSync(skillRoots[1]).filter((name) =>
398
+ name.startsWith('.agent-os-transaction-')).length, 1)
399
+ })
400
+
401
+ test('upgrades a schema 1 direct manifest while retaining managed integration state', async () => {
402
+ const root = temporaryRoot()
403
+ const homeDir = path.join(root, 'home')
404
+ const options = { command: 'install', platform: 'codex', method: 'direct', scope: 'user', policy: false }
405
+ await executeInstall(options, { output: silentOutput(), homeDir, cwd: root })
406
+ const skillsRoot = path.join(homeDir, '.codex', 'skills')
407
+ const manifestPath = path.join(skillsRoot, INSTALL_MANIFEST)
408
+ const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
409
+ delete manifest.integrations
410
+ manifest.schemaVersion = 1
411
+ fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\n')
412
+ await executeInstall({ ...options, command: 'update' }, { output: silentOutput(), homeDir, cwd: root })
413
+ const upgraded = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
414
+ assert.equal(upgraded.schemaVersion, 2)
415
+ assert.equal(upgraded.integrations.hooks.marker, '--agent-os-hook=quality-ratchet')
416
+ })
417
+
418
+ test('packaged hook asset and direct command use the stable runner marker', () => {
419
+ const packageJson = JSON.parse(fs.readFileSync(path.join(packageRootForTest(), 'package.json'), 'utf8'))
420
+ const hooks = JSON.parse(fs.readFileSync(path.join(packageRootForTest(), 'hooks', 'hooks.json'), 'utf8'))
421
+ const durableScript = path.join(temporaryRoot(), '.claude', 'skills', 'quality-ratchet', 'scripts', 'quality-delta.mjs')
422
+ const command = buildManagedHookCommand(durableScript)
423
+ const nativeHook = hooks.hooks.Stop[0].hooks[0]
424
+ assert.ok(packageJson.files.includes('hooks/'))
425
+ assert.equal(hooks.description.includes('quality-ratchet'), true)
426
+ assert.match(nativeHook.command, /\$\{CLAUDE_PLUGIN_ROOT\}/)
427
+ assert.equal(typeof nativeHook.commandWindows, 'string')
428
+ assert.equal(nativeHook.commandWindows.includes('"'), false)
429
+ const windowsMatch =
430
+ /^powershell\.exe -NoProfile -NonInteractive -EncodedCommand ([A-Za-z0-9+/]+={0,2})$/.exec(
431
+ nativeHook.commandWindows
432
+ )
433
+ assert.ok(windowsMatch)
434
+ const decodedWindowsCommand = Buffer.from(windowsMatch[1], 'base64').toString('utf16le')
435
+ assert.equal(decodedWindowsCommand,
436
+ "$runner = Join-Path $env:PLUGIN_ROOT 'skills\\quality-ratchet\\scripts\\quality-delta.mjs'; " +
437
+ 'node $runner hook --agent-os-hook=quality-ratchet')
438
+ assert.equal(command.startsWith('"' + process.execPath + '"'), true)
439
+ assert.equal(path.isAbsolute(durableScript), true)
440
+ assert.match(command, /--agent-os-hook=quality-ratchet/)
441
+ })
442
+
443
+ test('updates only managed skills and preserves unrelated skills', async () => {
444
+ const root = temporaryRoot()
445
+ const homeDir = path.join(root, 'home')
446
+ const options = {
447
+ command: 'install',
448
+ platform: 'codex',
449
+ method: 'direct',
450
+ scope: 'user',
451
+ policy: false
452
+ }
453
+ await executeInstall(options, { output: silentOutput(), homeDir, cwd: root })
454
+
455
+ const skillsRoot = path.join(homeDir, '.codex', 'skills')
456
+ const managedFile = path.join(skillsRoot, 'shape-work', 'SKILL.md')
457
+ const foreignFile = path.join(skillsRoot, 'my-private-skill', 'SKILL.md')
458
+ fs.writeFileSync(managedFile, 'stale managed content\n')
459
+ fs.mkdirSync(path.dirname(foreignFile), { recursive: true })
460
+ fs.writeFileSync(foreignFile, 'private content\n')
461
+
462
+ await executeInstall({ ...options, command: 'update' }, { output: silentOutput(), homeDir, cwd: root })
463
+ assert.notEqual(fs.readFileSync(managedFile, 'utf8'), 'stale managed content\n')
464
+ assert.equal(fs.readFileSync(foreignFile, 'utf8'), 'private content\n')
465
+ })
466
+
467
+ test('refuses to overwrite a same-name skill without an Agent OS manifest', async () => {
468
+ const root = temporaryRoot()
469
+ const homeDir = path.join(root, 'home')
470
+ const collision = path.join(homeDir, '.codex', 'skills', 'shape-work', 'SKILL.md')
471
+ fs.mkdirSync(path.dirname(collision), { recursive: true })
472
+ fs.writeFileSync(collision, 'developer-owned\n')
473
+
474
+ await assert.rejects(
475
+ executeInstall({
476
+ command: 'install',
477
+ platform: 'codex',
478
+ method: 'direct',
479
+ scope: 'user',
480
+ policy: false
481
+ }, { output: silentOutput(), homeDir, cwd: root }),
482
+ /Refusing to overwrite unmanaged skills.*shape-work/
483
+ )
484
+ assert.equal(fs.readFileSync(collision, 'utf8'), 'developer-owned\n')
485
+ assert.equal(fs.existsSync(path.join(homeDir, '.codex', 'skills', INSTALL_MANIFEST)), false)
486
+ })
487
+
488
+ test('policy planning preserves surrounding text and rejects malformed markers', () => {
489
+ const policy = '# Policy\n'
490
+ const original = 'before\n\n<!-- BEGIN AGENT OS -->\nold\n<!-- END AGENT OS -->\nafter\n'
491
+ const updated = planPolicyUpdate(original, policy)
492
+ assert.equal(updated.status, 'UPDATED')
493
+ assert.equal(updated.content, 'before\n\n<!-- BEGIN AGENT OS -->\n# Policy\n<!-- END AGENT OS -->\nafter\n')
494
+ assert.throws(
495
+ () => planPolicyUpdate('<!-- BEGIN AGENT OS -->\nbroken\n', policy),
496
+ /Malformed Agent OS policy markers/
497
+ )
498
+ })
499
+
500
+ test('malformed policy markers abort before direct skill installation', async () => {
501
+ const root = temporaryRoot()
502
+ const homeDir = path.join(root, 'home')
503
+ const target = path.join(homeDir, '.codex', 'AGENTS.md')
504
+ fs.mkdirSync(path.dirname(target), { recursive: true })
505
+ fs.writeFileSync(target, '<!-- BEGIN AGENT OS -->\nbroken\n')
506
+
507
+ await assert.rejects(
508
+ executeInstall({
509
+ command: 'install',
510
+ platform: 'codex',
511
+ method: 'direct',
512
+ scope: 'user',
513
+ policy: true
514
+ }, { output: silentOutput(), homeDir, cwd: root }),
515
+ /Malformed Agent OS policy markers/
516
+ )
517
+ assert.equal(fs.existsSync(path.join(homeDir, '.codex', 'skills')), false)
518
+ })
519
+
520
+ test('plugin mode still requires only the selected host CLI', async () => {
521
+ const calls = []
522
+ await assert.rejects(
523
+ executeInstall({
524
+ command: 'install',
525
+ platform: 'codex',
526
+ method: 'plugin',
527
+ scope: 'user',
528
+ policy: false
529
+ }, {
530
+ output: silentOutput(),
531
+ exists: () => false,
532
+ execute: (...args) => calls.push(args)
533
+ }),
534
+ /Native plugin mode requires the selected host CLIs.*Codex/s
535
+ )
536
+ assert.deepEqual(calls, [])
537
+ })
538
+
539
+ test('plugin update skips Git refresh for a registered local Codex marketplace', async () => {
540
+ const calls = []
541
+ await executeInstall({
542
+ command: 'update',
543
+ platform: 'codex',
544
+ method: 'plugin',
545
+ scope: 'user',
546
+ policy: false
547
+ }, {
548
+ output: silentOutput(),
549
+ exists: () => true,
550
+ execute: (command, args) => {
551
+ calls.push([command, args])
552
+ if (args.join(' ') === 'plugin marketplace list --json') {
553
+ return { status: 0, stdout: JSON.stringify({ marketplaces: [{ name: 'agent-os' }] }), stderr: '' }
554
+ }
555
+ if (args.join(' ') === 'plugin list --json') {
556
+ return {
557
+ status: 0,
558
+ stdout: JSON.stringify({ installed: [{
559
+ marketplaceName: 'agent-os',
560
+ marketplaceSource: { sourceType: 'local' }
561
+ }] }),
562
+ stderr: ''
563
+ }
564
+ }
565
+ return { status: 0, stdout: '', stderr: '' }
566
+ }
567
+ })
568
+
569
+ assert.deepEqual(calls.map((call) => call[1]), [
570
+ ['plugin', 'marketplace', 'list', '--json'],
571
+ ['plugin', 'list', '--json'],
572
+ ['plugin', 'add', 'agent-os@agent-os']
573
+ ])
574
+ })