@notis_ai/cli 0.2.0-beta.16.1 → 0.2.0-beta.160.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 (154) hide show
  1. package/README.md +433 -133
  2. package/config/notis_app_boundary_rules.json +50 -0
  3. package/config/notis_app_design_rules.json +135 -0
  4. package/dist/agent-hooks/notis-agent-hook.mjs +18672 -0
  5. package/dist/base-skills/notis-apps/SKILL.md +70 -0
  6. package/dist/base-skills/notis-apps/references/architecture.md +164 -0
  7. package/dist/base-skills/notis-apps/references/context.md +81 -0
  8. package/dist/base-skills/notis-apps/references/design.md +165 -0
  9. package/dist/base-skills/notis-apps/references/reading.md +89 -0
  10. package/dist/base-skills/notis-apps/references/release.md +99 -0
  11. package/dist/base-skills/notis-apps/references/sdk.md +62 -0
  12. package/dist/base-skills/notis-apps/references/troubleshooting.md +23 -0
  13. package/dist/base-skills/notis-cli/SKILL.md +140 -0
  14. package/dist/base-skills/notis-cli/references/app-delivery.md +18 -0
  15. package/dist/base-skills/notis-cli/references/native-databases.md +20 -0
  16. package/dist/base-skills/notis-cli/references/tool-examples.md +56 -0
  17. package/dist/base-skills/notis-cli/references/troubleshooting.md +39 -0
  18. package/dist/base-skills/notis-query/SKILL.md +67 -0
  19. package/dist/base-skills/notis-query/references/database-discovery.md +59 -0
  20. package/dist/base-skills/notis-query/references/documents.md +50 -0
  21. package/dist/base-skills/notis-query/references/query.md +543 -0
  22. package/dist/skill-sync/index.js +1626 -0
  23. package/dist/skill-sync/index.js.map +7 -0
  24. package/dist/skill-sync-worker.mjs +2990 -0
  25. package/package.json +16 -6
  26. package/skills/notis-apps/cli.md +313 -0
  27. package/skills/notis-cli/AGENT_INSTRUCTIONS.md +39 -0
  28. package/skills/notis-onboarding/BRIEF.md +129 -0
  29. package/skills/notis-query/cli.md +39 -0
  30. package/src/agent-hook-entry.js +5 -0
  31. package/src/cli.js +294 -25
  32. package/src/command-specs/agents.js +392 -0
  33. package/src/command-specs/apps.js +1470 -202
  34. package/src/command-specs/auth.js +114 -137
  35. package/src/command-specs/diagnostics.js +716 -0
  36. package/src/command-specs/handover.js +374 -0
  37. package/src/command-specs/helpers.js +84 -82
  38. package/src/command-specs/index.js +25 -6
  39. package/src/command-specs/meta.js +150 -18
  40. package/src/command-specs/onboarding.js +290 -0
  41. package/src/command-specs/profile.js +358 -0
  42. package/src/command-specs/reports.js +86 -0
  43. package/src/command-specs/skills.js +75 -0
  44. package/src/command-specs/smoke.js +386 -0
  45. package/src/command-specs/tools.js +455 -139
  46. package/src/runtime/agent-browser.js +632 -0
  47. package/src/runtime/agent-memory-state.js +126 -0
  48. package/src/runtime/agent-setup.js +383 -0
  49. package/src/runtime/app-boundary-validator.js +404 -0
  50. package/src/runtime/app-changelog.js +79 -0
  51. package/src/runtime/app-platform.js +2633 -210
  52. package/src/runtime/app-registry-scaffolds.js +367 -0
  53. package/src/runtime/app-test-server.js +292 -0
  54. package/src/runtime/assets/store-screenshot-dark.png +0 -0
  55. package/src/runtime/auth-recovery.js +110 -0
  56. package/src/runtime/base-skills.d.ts +20 -0
  57. package/src/runtime/base-skills.js +167 -0
  58. package/src/runtime/channel.js +133 -0
  59. package/src/runtime/delegated-context.js +68 -0
  60. package/src/runtime/errors.js +1 -0
  61. package/src/runtime/git.js +233 -0
  62. package/src/runtime/login-listener.js +15 -0
  63. package/src/runtime/oauth.js +2622 -0
  64. package/src/runtime/output.js +37 -5
  65. package/src/runtime/ports.js +31 -0
  66. package/src/runtime/profiles.js +906 -55
  67. package/src/runtime/skill-sync/cloud-client.ts +99 -0
  68. package/src/runtime/skill-sync/index.ts +697 -0
  69. package/src/runtime/skill-sync/local-scanner.ts +1046 -0
  70. package/src/runtime/skill-sync/symlink-manager.ts +433 -0
  71. package/src/runtime/skill-sync/sync-plan.ts +22 -0
  72. package/src/runtime/skill-sync/types.ts +110 -0
  73. package/src/runtime/skill-sync/write-cloud-skill.ts +50 -0
  74. package/src/runtime/skill-sync-service.js +109 -0
  75. package/src/runtime/store-screenshot.js +143 -0
  76. package/src/runtime/sync-skills.d.ts +37 -0
  77. package/src/runtime/sync-skills.js +231 -0
  78. package/src/runtime/telemetry.js +92 -0
  79. package/src/runtime/transport.js +324 -45
  80. package/src/skill-sync-worker-entry.js +2 -0
  81. package/src/skill-sync-worker.js +50 -0
  82. package/template/.harness/index.html.tmpl +430 -0
  83. package/template/CHANGELOG.md +5 -0
  84. package/template/app/layout.tsx +5 -2
  85. package/template/app/page.tsx +49 -42
  86. package/template/components/page-heading.tsx +23 -0
  87. package/template/components/ui/badge.tsx +7 -4
  88. package/template/components/ui/card.tsx +24 -11
  89. package/template/components/ui/native-select.tsx +24 -0
  90. package/template/notis.config.ts +24 -6
  91. package/template/package-lock.json +4137 -0
  92. package/template/package.json +5 -5
  93. package/template/packages/{notis-sdk → sdk}/package.json +13 -3
  94. package/template/packages/sdk/src/agentContext.ts +36 -0
  95. package/template/packages/sdk/src/components/DocumentEditor.tsx +103 -0
  96. package/template/packages/sdk/src/components/Markdown.tsx +60 -0
  97. package/template/packages/sdk/src/components/MarkdownEditor.tsx +121 -0
  98. package/template/packages/sdk/src/components/MultiSelectActionBar.tsx +285 -0
  99. package/template/packages/sdk/src/components/MultiSelectCheckbox.tsx +97 -0
  100. package/template/packages/sdk/src/components/MultiSelectDragOverlay.tsx +39 -0
  101. package/template/packages/sdk/src/components/NotisCommentBoundary.tsx +172 -0
  102. package/template/packages/sdk/src/components/NotisSelectionBoundary.tsx +59 -0
  103. package/template/packages/sdk/src/components/ShortcutHints.tsx +56 -0
  104. package/template/packages/sdk/src/components/Skeleton.tsx +24 -0
  105. package/template/packages/sdk/src/config.ts +257 -0
  106. package/template/packages/sdk/src/documents.ts +256 -0
  107. package/template/packages/sdk/src/hooks/useActiveResource.ts +19 -0
  108. package/template/packages/sdk/src/hooks/useAgentContext.ts +23 -0
  109. package/template/packages/sdk/src/hooks/useCloudComputer.ts +64 -0
  110. package/template/packages/sdk/src/hooks/useCollectionInteractions.ts +836 -0
  111. package/template/packages/sdk/src/hooks/useDatabaseSchema.ts +49 -0
  112. package/template/packages/sdk/src/hooks/useDatabaseSubscription.ts +76 -0
  113. package/template/packages/sdk/src/hooks/useDocument.ts +43 -0
  114. package/template/packages/sdk/src/hooks/useDocuments.ts +84 -0
  115. package/template/packages/sdk/src/hooks/useHandover.ts +78 -0
  116. package/template/packages/sdk/src/hooks/useLongPressSelection.ts +79 -0
  117. package/template/packages/sdk/src/hooks/useMultiSelect.ts +95 -0
  118. package/template/packages/{notis-sdk → sdk}/src/hooks/useNotis.ts +10 -4
  119. package/template/packages/{notis-sdk → sdk}/src/hooks/useNotisNavigation.ts +11 -8
  120. package/template/packages/sdk/src/hooks/useQuery.ts +71 -0
  121. package/template/packages/sdk/src/hooks/useTool.ts +65 -0
  122. package/template/packages/sdk/src/hooks/useToolQuery.ts +12 -0
  123. package/template/packages/sdk/src/hooks/useTopBarSearch.ts +81 -0
  124. package/template/packages/sdk/src/hooks/useUpsertDocument.ts +95 -0
  125. package/template/packages/sdk/src/index.ts +161 -0
  126. package/template/packages/sdk/src/interactions/actions.ts +59 -0
  127. package/template/packages/sdk/src/interactions/shortcuts.tsx +694 -0
  128. package/template/packages/sdk/src/interactions/visibility.ts +13 -0
  129. package/template/packages/sdk/src/interactions.ts +45 -0
  130. package/template/packages/sdk/src/provider.tsx +44 -0
  131. package/template/packages/sdk/src/queryCache.ts +170 -0
  132. package/template/packages/sdk/src/runtime.ts +451 -0
  133. package/template/packages/sdk/src/styles.css +213 -0
  134. package/template/packages/sdk/src/tailwind.ts +56 -0
  135. package/template/packages/{notis-sdk → sdk}/src/vite.ts +5 -1
  136. package/template/tailwind.config.ts +1 -0
  137. package/src/command-specs/db.js +0 -163
  138. package/src/runtime/app-preview-server.js +0 -312
  139. package/template/packages/notis-sdk/src/config.ts +0 -48
  140. package/template/packages/notis-sdk/src/helpers.ts +0 -131
  141. package/template/packages/notis-sdk/src/hooks/useAppState.ts +0 -50
  142. package/template/packages/notis-sdk/src/hooks/useCollectionItem.ts +0 -58
  143. package/template/packages/notis-sdk/src/hooks/useDatabase.ts +0 -87
  144. package/template/packages/notis-sdk/src/hooks/useDocument.ts +0 -61
  145. package/template/packages/notis-sdk/src/hooks/useTool.ts +0 -49
  146. package/template/packages/notis-sdk/src/hooks/useUpsertDocument.ts +0 -57
  147. package/template/packages/notis-sdk/src/index.ts +0 -47
  148. package/template/packages/notis-sdk/src/provider.tsx +0 -44
  149. package/template/packages/notis-sdk/src/runtime.ts +0 -159
  150. package/template/packages/notis-sdk/src/styles.css +0 -123
  151. /package/template/packages/{notis-sdk → sdk}/src/hooks/useBackend.ts +0 -0
  152. /package/template/packages/{notis-sdk → sdk}/src/hooks/useTools.ts +0 -0
  153. /package/template/packages/{notis-sdk → sdk}/src/ui.ts +0 -0
  154. /package/template/packages/{notis-sdk → sdk}/tsconfig.json +0 -0
@@ -0,0 +1,126 @@
1
+ import { createHash } from 'node:crypto';
2
+ import {
3
+ existsSync,
4
+ mkdirSync,
5
+ readFileSync,
6
+ renameSync,
7
+ writeFileSync,
8
+ } from 'node:fs';
9
+ import { homedir } from 'node:os';
10
+ import { dirname, join } from 'node:path';
11
+
12
+ const MAX_SEEN_HASHES = 500;
13
+ const MAX_PROMPT_CHARS = 12_000;
14
+
15
+ function hash(value) {
16
+ return createHash('sha256').update(String(value || '')).digest('hex').slice(0, 24);
17
+ }
18
+
19
+ function statePath(sessionId, home = homedir()) {
20
+ if (!sessionId) return null;
21
+ return join(home, '.notis', 'agent-memory-hooks', `${hash(sessionId)}.json`);
22
+ }
23
+
24
+ function readState(sessionId, home) {
25
+ const filePath = statePath(sessionId, home);
26
+ if (!filePath || !existsSync(filePath)) return { seen: [], pending: null };
27
+ try {
28
+ const parsed = JSON.parse(readFileSync(filePath, 'utf-8'));
29
+ return parsed && typeof parsed === 'object'
30
+ ? {
31
+ seen: Array.isArray(parsed.seen) ? parsed.seen.filter((item) => typeof item === 'string') : [],
32
+ pending: parsed.pending && typeof parsed.pending === 'object' ? parsed.pending : null,
33
+ identity: parsed.identity && typeof parsed.identity === 'object' ? parsed.identity : null,
34
+ }
35
+ : { seen: [], pending: null };
36
+ } catch {
37
+ return { seen: [], pending: null };
38
+ }
39
+ }
40
+
41
+ function inputIdentity(input) {
42
+ const profile = typeof input?.profile_name === 'string' ? input.profile_name : '';
43
+ const account = typeof input?.account_id === 'string' ? input.account_id : '';
44
+ const apiBase = typeof input?.api_base === 'string' ? input.api_base : '';
45
+ return profile && account && apiBase ? { profile, account, apiBase } : null;
46
+ }
47
+
48
+ function identityMatches(left, right) {
49
+ return (!left && !right) || Boolean(
50
+ left && right
51
+ && left.profile === right.profile
52
+ && left.account === right.account
53
+ && left.apiBase === right.apiBase,
54
+ );
55
+ }
56
+
57
+ function stateForInput(sessionId, input, home) {
58
+ const state = readState(sessionId, home);
59
+ const identity = inputIdentity(input);
60
+ if (identityMatches(state.identity, identity)) return state;
61
+ return { seen: [], pending: null, identity };
62
+ }
63
+
64
+ function writeState(sessionId, state, home) {
65
+ const filePath = statePath(sessionId, home);
66
+ if (!filePath) return;
67
+ mkdirSync(dirname(filePath), { recursive: true, mode: 0o700 });
68
+ const temporaryPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
69
+ writeFileSync(temporaryPath, `${JSON.stringify(state)}\n`, { mode: 0o600 });
70
+ renameSync(temporaryPath, filePath);
71
+ }
72
+
73
+ export function rememberPendingTurn(input, home = homedir()) {
74
+ const sessionId = input?.session_id;
75
+ const prompt = typeof input?.prompt === 'string' ? input.prompt.trim() : '';
76
+ if (!sessionId || !prompt || ['/', '!', '#'].includes(prompt[0])) return;
77
+ const state = stateForInput(sessionId, input, home);
78
+ state.pending = {
79
+ prompt: prompt.slice(0, MAX_PROMPT_CHARS),
80
+ turn_id: typeof input?.turn_id === 'string' ? input.turn_id : null,
81
+ cwd: typeof input?.cwd === 'string' ? input.cwd : null,
82
+ recorded_at: new Date().toISOString(),
83
+ };
84
+ writeState(sessionId, state, home);
85
+ }
86
+
87
+ export function pendingTurn(input, home = homedir()) {
88
+ const sessionId = input?.session_id;
89
+ const state = readState(sessionId, home);
90
+ const identity = inputIdentity(input);
91
+ if (!identityMatches(state.identity, identity)) {
92
+ if (sessionId) writeState(sessionId, { seen: [], pending: null, identity }, home);
93
+ return null;
94
+ }
95
+ if (!state.pending) return null;
96
+ if (state.pending.turn_id && input?.turn_id && state.pending.turn_id !== input.turn_id) return null;
97
+ return state.pending;
98
+ }
99
+
100
+ export function completePendingTurn(input, home = homedir()) {
101
+ const sessionId = input?.session_id;
102
+ if (!sessionId) return;
103
+ const filePath = statePath(sessionId, home);
104
+ if (!filePath || !existsSync(filePath)) return;
105
+ const state = readState(sessionId, home);
106
+ if (!state.pending) return;
107
+ state.pending = null;
108
+ writeState(sessionId, state, home);
109
+ }
110
+
111
+ export function freshRecallItems(input, items, home = homedir()) {
112
+ const sessionId = input?.session_id;
113
+ if (!sessionId) return items;
114
+ const state = stateForInput(sessionId, input, home);
115
+ const seen = new Set(state.seen);
116
+ const fresh = [];
117
+ for (const item of items) {
118
+ const fingerprint = hash(item);
119
+ if (seen.has(fingerprint)) continue;
120
+ seen.add(fingerprint);
121
+ fresh.push(item);
122
+ }
123
+ state.seen = [...seen].slice(-MAX_SEEN_HASHES);
124
+ writeState(sessionId, state, home);
125
+ return fresh;
126
+ }
@@ -0,0 +1,383 @@
1
+ import {
2
+ chmodSync,
3
+ existsSync,
4
+ mkdirSync,
5
+ readFileSync,
6
+ readdirSync,
7
+ renameSync,
8
+ writeFileSync,
9
+ } from 'node:fs';
10
+ import { createHash } from 'node:crypto';
11
+ import { homedir } from 'node:os';
12
+ import { dirname, join } from 'node:path';
13
+ import { fileURLToPath } from 'node:url';
14
+
15
+ const HERE = dirname(fileURLToPath(import.meta.url));
16
+ const INSTRUCTIONS_PATH = join(HERE, '..', '..', 'skills', 'notis-cli', 'AGENT_INSTRUCTIONS.md');
17
+ const HOOK_BUNDLE_PATH = join(HERE, '..', '..', 'dist', 'agent-hooks', 'notis-agent-hook.mjs');
18
+ const START_MARKER = '<!-- notis-cli:instructions:start -->';
19
+ const END_MARKER = '<!-- notis-cli:instructions:end -->';
20
+ const LEGACY_SENTINEL = 'Use the Notis CLI (`npx --package @notis_ai/cli@latest -- notis ...`)';
21
+ const MANAGED_HOOK_MARKER = '--notis-managed-agent-hook';
22
+ const LEGACY_MANAGED_HOOK_MARKER = 'NOTIS_MANAGED_AGENT_HOOK=1';
23
+
24
+ export const AGENT_IDS = Object.freeze(['codex', 'claude-code']);
25
+
26
+ function atomicWrite(filePath, contents, mode = 0o600) {
27
+ mkdirSync(dirname(filePath), { recursive: true, mode: 0o700 });
28
+ const temporaryPath = `${filePath}.notis-${process.pid}-${Date.now()}.tmp`;
29
+ writeFileSync(temporaryPath, contents, { mode });
30
+ renameSync(temporaryPath, filePath);
31
+ }
32
+
33
+ function readText(filePath) {
34
+ return existsSync(filePath) ? readFileSync(filePath, 'utf-8') : '';
35
+ }
36
+
37
+ function activeCodexInstructionsPath(home) {
38
+ const overridePath = join(home, '.codex', 'AGENTS.override.md');
39
+ if (existsSync(overridePath) && readText(overridePath).trim()) {
40
+ return overridePath;
41
+ }
42
+ return join(home, '.codex', 'AGENTS.md');
43
+ }
44
+
45
+ function upsertInstructionBlock(filePath, block) {
46
+ const existing = readText(filePath);
47
+ const managedBlockPattern = new RegExp(
48
+ `${START_MARKER.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[\\s\\S]*?${END_MARKER.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`,
49
+ 'g',
50
+ );
51
+ const managedBlocks = [...existing.matchAll(managedBlockPattern)];
52
+ let next;
53
+ let status;
54
+
55
+ if (managedBlocks.length) {
56
+ let replaced = false;
57
+ next = existing.replace(managedBlockPattern, () => {
58
+ if (replaced) return '';
59
+ replaced = true;
60
+ return block;
61
+ });
62
+ status = next === existing ? 'unchanged' : 'updated';
63
+ } else if (existing.includes(LEGACY_SENTINEL)) {
64
+ // An unmarked block belongs to the user or another installer. Do not risk
65
+ // rewriting adjacent personal instructions merely to take ownership of it.
66
+ return { path: filePath, status: 'already_present_unmanaged' };
67
+ } else {
68
+ const prefix = existing.trimEnd();
69
+ next = `${prefix ? `${prefix}\n\n` : ''}${block.trim()}\n`;
70
+ status = 'installed';
71
+ }
72
+
73
+ if (status !== 'unchanged') {
74
+ atomicWrite(filePath, next);
75
+ }
76
+ return { path: filePath, status };
77
+ }
78
+
79
+ function readJsonObject(filePath) {
80
+ if (!existsSync(filePath)) return {};
81
+ const raw = readFileSync(filePath, 'utf-8');
82
+ try {
83
+ const parsed = JSON.parse(raw);
84
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
85
+ throw new Error('root value must be an object');
86
+ }
87
+ return parsed;
88
+ } catch (error) {
89
+ throw new Error(`Refusing to modify invalid JSON at ${filePath}: ${error.message}`);
90
+ }
91
+ }
92
+
93
+ function isManagedNotisHookCommand(command) {
94
+ if (typeof command !== 'string') return false;
95
+ if (
96
+ command.includes(MANAGED_HOOK_MARKER)
97
+ || command.includes(LEGACY_MANAGED_HOOK_MARKER)
98
+ ) return true;
99
+ if (
100
+ command.includes('.notis/agent-hooks/bin/notis-agent-hook')
101
+ && /\sagent-(?:context|capture)\b/.test(command)
102
+ ) return true;
103
+ // Supported legacy installers used either this package's bin/notis.js path
104
+ // or the exact public npx package. Do not claim arbitrary commands merely
105
+ // because they happen to use the same subcommand words.
106
+ return (
107
+ /(?:^|\s)(?:'[^']*|"[^"]*"|\S*)bin\/notis\.js(?:'|")?\s[^\n]*\sagent-(?:context|capture)\b/.test(command)
108
+ || /@notis_ai\/cli@[^\s]+\s+--\s+notis\s[^\n]*\sagent-(?:context|capture)\b/.test(command)
109
+ || /^\s*notis\s+(?:--[^\s]+\s+)*agent-(?:context|capture)\b/.test(command)
110
+ );
111
+ }
112
+
113
+ function objectHasManagedNotisHook(value) {
114
+ if (Array.isArray(value)) return value.some(objectHasManagedNotisHook);
115
+ if (!value || typeof value !== 'object') return false;
116
+ if (isManagedNotisHookCommand(value.command)) return true;
117
+ return Object.values(value).some(objectHasManagedNotisHook);
118
+ }
119
+
120
+ function fileHasManagedNotisHook(filePath) {
121
+ try {
122
+ return objectHasManagedNotisHook(readJsonObject(filePath));
123
+ } catch {
124
+ return false;
125
+ }
126
+ }
127
+
128
+ function removePriorNotisPromptHooks(groups) {
129
+ if (!Array.isArray(groups)) return [];
130
+ const kept = [];
131
+ for (const group of groups) {
132
+ if (!group || typeof group !== 'object' || !Array.isArray(group.hooks)) {
133
+ kept.push(group);
134
+ continue;
135
+ }
136
+ const hooks = group.hooks.filter((hook) => !isManagedNotisHookCommand(hook?.command));
137
+ if (hooks.length) kept.push({ ...group, hooks });
138
+ }
139
+ return kept;
140
+ }
141
+
142
+ function shellQuote(value) {
143
+ return `'${String(value).replaceAll("'", `'"'"'`)}'`;
144
+ }
145
+
146
+ export function installManagedHookRuntime({
147
+ home = homedir(),
148
+ bundlePath = HOOK_BUNDLE_PATH,
149
+ nodePath = process.execPath,
150
+ platform = process.platform,
151
+ } = {}) {
152
+ const bundle = readFileSync(bundlePath);
153
+ const digest = createHash('sha256').update(bundle).digest('hex');
154
+ const runtimePath = join(
155
+ home,
156
+ '.notis',
157
+ 'agent-hooks',
158
+ 'runtime',
159
+ digest,
160
+ 'notis-agent-hook.mjs',
161
+ );
162
+ const existingDigest = existsSync(runtimePath)
163
+ ? createHash('sha256').update(readFileSync(runtimePath)).digest('hex')
164
+ : null;
165
+ if (existingDigest !== digest) atomicWrite(runtimePath, bundle, 0o500);
166
+ chmodSync(runtimePath, 0o500);
167
+
168
+ const launcherPath = join(
169
+ home,
170
+ '.notis',
171
+ 'agent-hooks',
172
+ 'bin',
173
+ platform === 'win32' ? 'notis-agent-hook.cmd' : 'notis-agent-hook',
174
+ );
175
+ const launcher = platform === 'win32'
176
+ ? [
177
+ '@echo off',
178
+ `if not exist "${nodePath}" (`,
179
+ ' >&2 echo Notis memory hook runtime needs repair; run notis agents install again.',
180
+ ' exit /b 1',
181
+ ')',
182
+ `"${nodePath}" "${runtimePath}" %*`,
183
+ '',
184
+ ].join('\r\n')
185
+ : [
186
+ '#!/bin/sh',
187
+ `if [ -x ${shellQuote(nodePath)} ]; then`,
188
+ ` exec ${shellQuote(nodePath)} ${shellQuote(runtimePath)} "$@"`,
189
+ 'fi',
190
+ `echo 'Notis memory hook runtime needs repair; run notis agents install again.' >&2`,
191
+ 'exit 1',
192
+ '',
193
+ ].join('\n');
194
+ if (readText(launcherPath) !== launcher) {
195
+ // Windows maps the owner write bit to the read-only file attribute. A
196
+ // launcher installed by an older CLI is deliberately read-only, so make
197
+ // our owned file replaceable before the atomic rename used for upgrades.
198
+ if (platform === 'win32' && existsSync(launcherPath)) {
199
+ chmodSync(launcherPath, 0o700);
200
+ }
201
+ atomicWrite(launcherPath, launcher, 0o500);
202
+ }
203
+ chmodSync(launcherPath, 0o500);
204
+ return { launcherPath, runtimePath, digest };
205
+ }
206
+
207
+ function shellHookCommand(launcherPath, profileName, command, agentId = null, platform = process.platform) {
208
+ // Stored profile names follow a shell-safe grammar enforced by the CLI.
209
+ const agentFlag = agentId ? ` --agent ${agentId}` : '';
210
+ const quotedLauncher = platform === 'win32'
211
+ ? `"${launcherPath}"`
212
+ : shellQuote(launcherPath);
213
+ return `${quotedLauncher} ${MANAGED_HOOK_MARKER} --profile ${profileName} --timeout-ms 30000 ${command}${agentFlag}`;
214
+ }
215
+
216
+ function contextHandler(agentId, profileName, launcherPath, platform) {
217
+ return {
218
+ type: 'command',
219
+ command: shellHookCommand(launcherPath, profileName, 'agent-context', null, platform),
220
+ timeout: 30,
221
+ statusMessage: 'Loading relevant Notis memory',
222
+ ...(agentId === 'codex' ? { additionalContextLimit: 2500 } : {}),
223
+ };
224
+ }
225
+
226
+ function captureHandler(agentId, profileName, launcherPath, platform) {
227
+ return {
228
+ type: 'command',
229
+ command: shellHookCommand(launcherPath, profileName, 'agent-capture', agentId, platform),
230
+ timeout: 30,
231
+ statusMessage: 'Saving durable Notis context',
232
+ };
233
+ }
234
+
235
+ function upsertPromptHooks(
236
+ filePath,
237
+ agentId,
238
+ profileName,
239
+ enabled,
240
+ launcherPath = '',
241
+ platform = process.platform,
242
+ ) {
243
+ const settings = readJsonObject(filePath);
244
+ const hooks = settings.hooks && typeof settings.hooks === 'object' && !Array.isArray(settings.hooks)
245
+ ? { ...settings.hooks }
246
+ : {};
247
+ let removedManagedHook = false;
248
+ for (const event of ['SessionStart', 'UserPromptSubmit', 'Stop']) {
249
+ const previousGroups = Array.isArray(hooks[event]) ? hooks[event] : [];
250
+ const groups = removePriorNotisPromptHooks(previousGroups);
251
+ if (JSON.stringify(groups) !== JSON.stringify(previousGroups)) removedManagedHook = true;
252
+ if (enabled) {
253
+ groups.push({ hooks: [event === 'Stop'
254
+ ? captureHandler(agentId, profileName, launcherPath, platform)
255
+ : contextHandler(agentId, profileName, launcherPath, platform)] });
256
+ }
257
+ if (groups.length) hooks[event] = groups;
258
+ else delete hooks[event];
259
+ }
260
+ const next = { ...settings, hooks };
261
+ const serialized = `${JSON.stringify(next, null, 2)}\n`;
262
+ const existing = readText(filePath);
263
+ if (!enabled && !removedManagedHook) return { path: filePath, status: 'unchanged' };
264
+ if (existing === serialized) return { path: filePath, status: 'unchanged' };
265
+ atomicWrite(filePath, serialized);
266
+ return {
267
+ path: filePath,
268
+ status: enabled
269
+ ? existsSync(filePath) && existing ? 'updated' : 'installed'
270
+ : 'removed',
271
+ };
272
+ }
273
+
274
+ function agentPaths(agentId, home) {
275
+ if (agentId === 'codex') {
276
+ return {
277
+ root: join(home, '.codex'),
278
+ instructions: activeCodexInstructionsPath(home),
279
+ hooks: join(home, '.codex', 'hooks.json'),
280
+ };
281
+ }
282
+ if (agentId === 'claude-code') {
283
+ return {
284
+ root: join(home, '.claude'),
285
+ instructions: join(home, '.claude', 'CLAUDE.md'),
286
+ hooks: join(home, '.claude', 'settings.json'),
287
+ };
288
+ }
289
+ throw new Error(`Unsupported agent: ${agentId}`);
290
+ }
291
+
292
+ export function shouldInstallLocalAgentSetup(env = process.env, home = homedir()) {
293
+ if (env.NOTIS_JWT || env.NOTIS_AGENT === '1' || env.NOTIS_DELEGATED_CONTEXT === '1') return false;
294
+ if (env.CONDUCTOR_IS_LOCAL === '0') return false;
295
+ if (existsSync('/vercel/sandbox')) return false;
296
+ return Boolean(home);
297
+ }
298
+
299
+ export function installAgentSetup({
300
+ profileName,
301
+ agents = AGENT_IDS,
302
+ memoryHooks = true,
303
+ onlyExisting = false,
304
+ detectedAgents = null,
305
+ home = homedir(),
306
+ platform = process.platform,
307
+ } = {}) {
308
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(String(profileName || ''))) {
309
+ throw new Error('A valid authenticated Notis profile is required for agent setup');
310
+ }
311
+ const instructions = readFileSync(INSTRUCTIONS_PATH, 'utf-8').trim();
312
+ const results = [];
313
+ const shouldRepairManagedHooks = memoryHooks === true || (
314
+ memoryHooks === null
315
+ && agents.some((agentId) => fileHasManagedNotisHook(agentPaths(agentId, home).hooks))
316
+ );
317
+ const hookRuntime = shouldRepairManagedHooks
318
+ ? installManagedHookRuntime({ home, platform })
319
+ : null;
320
+
321
+ for (const agentId of agents) {
322
+ const paths = agentPaths(agentId, home);
323
+ const detected = Array.isArray(detectedAgents)
324
+ ? detectedAgents.includes(agentId)
325
+ : existsSync(paths.root);
326
+ if (onlyExisting && !detected) {
327
+ results.push({ agent: agentId, status: 'not_detected' });
328
+ continue;
329
+ }
330
+ const result = {
331
+ agent: agentId,
332
+ instructions: upsertInstructionBlock(paths.instructions, instructions),
333
+ memory_hook: null,
334
+ };
335
+ if (memoryHooks === null && !fileHasManagedNotisHook(paths.hooks)) {
336
+ result.memory_hook = { path: paths.hooks, status: 'preserved' };
337
+ } else {
338
+ try {
339
+ result.memory_hook = upsertPromptHooks(
340
+ paths.hooks,
341
+ agentId,
342
+ profileName,
343
+ memoryHooks !== false,
344
+ hookRuntime?.launcherPath,
345
+ platform,
346
+ );
347
+ } catch (error) {
348
+ result.memory_hook = {
349
+ path: paths.hooks,
350
+ status: 'error',
351
+ message: error instanceof Error ? error.message : String(error),
352
+ };
353
+ }
354
+ }
355
+ results.push(result);
356
+ }
357
+ return results;
358
+ }
359
+
360
+ export function detectedAgentIds(home = homedir()) {
361
+ return AGENT_IDS.filter((agentId) => {
362
+ const root = agentPaths(agentId, home).root;
363
+ if (!existsSync(root)) return false;
364
+ try {
365
+ const entries = readdirSync(root, { withFileTypes: true });
366
+ if (entries.some((entry) => entry.name !== 'skills')) return true;
367
+ const skillsRoot = join(root, 'skills');
368
+ if (!existsSync(skillsRoot)) return false;
369
+ // Base reconciliation intentionally creates every supported vendor root.
370
+ // A root containing only our three links is not evidence that the vendor
371
+ // itself is installed; any other skill entry is a genuine user signal.
372
+ return readdirSync(skillsRoot).some((name) => (
373
+ !['notis-apps', 'notis-query', 'notis-cli'].includes(name)
374
+ ));
375
+ } catch {
376
+ return false;
377
+ }
378
+ });
379
+ }
380
+
381
+ export function instructionTemplatePath() {
382
+ return INSTRUCTIONS_PATH;
383
+ }