@tinchu0/knitt 0.1.0-beta.4 → 0.1.0-beta.5

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 (40) hide show
  1. package/README.md +26 -15
  2. package/dist/apps/server/src/conversation-server.js +203 -0
  3. package/dist/apps/server/src/http-gateway.js +1 -1
  4. package/dist/apps/server/src/main.js +6 -2
  5. package/dist/apps/server/src/server.js +292 -81
  6. package/dist/memory-schema.json +1 -0
  7. package/dist/packages/adapters/src/cli.js +193 -45
  8. package/dist/packages/adapters/src/command.js +149 -18
  9. package/dist/packages/adapters/src/discovery.js +128 -0
  10. package/dist/packages/adapters/src/handoff.js +13 -8
  11. package/dist/packages/adapters/src/histories.js +222 -0
  12. package/dist/packages/adapters/src/history-time.js +10 -0
  13. package/dist/packages/adapters/src/hooks.js +123 -44
  14. package/dist/packages/adapters/src/launch.js +10 -3
  15. package/dist/packages/adapters/src/picker.js +2 -1
  16. package/dist/packages/adapters/src/providers/jsonl.js +105 -0
  17. package/dist/packages/adapters/src/question-ui.js +37 -0
  18. package/dist/packages/adapters/src/repository.js +32 -5
  19. package/dist/packages/adapters/src/scope.js +9 -3
  20. package/dist/packages/adapters/src/server-name.js +6 -0
  21. package/dist/packages/adapters/src/status.js +81 -0
  22. package/dist/packages/adapters/src/transcript.js +72 -15
  23. package/dist/packages/contracts/src/conversations.js +30 -0
  24. package/dist/packages/installer/src/antigravity.js +90 -0
  25. package/dist/packages/installer/src/cli.js +210 -48
  26. package/dist/packages/installer/src/connections.js +193 -38
  27. package/dist/packages/installer/src/runtime.js +73 -11
  28. package/dist/packages/memory/src/checkpoints.js +68 -0
  29. package/dist/packages/memory/src/context.js +91 -0
  30. package/dist/packages/memory/src/conversations.js +148 -0
  31. package/dist/packages/memory/src/migrations.js +55 -0
  32. package/dist/packages/memory/src/store.js +201 -23
  33. package/dist/skills/continue-chat/SKILL.md +15 -0
  34. package/dist/skills/knit/SKILL.md +11 -5
  35. package/docs/clients/connector-setup.md +1 -1
  36. package/docs/clients/local-agent-memory.md +50 -0
  37. package/docs/clients/npm-installation.md +23 -11
  38. package/docs/clients/support-matrix.md +12 -0
  39. package/docs/usage.md +19 -29
  40. package/package.json +5 -4
@@ -1,15 +1,20 @@
1
1
  #!/usr/bin/env node
2
+ import { readCaptureStatuses } from '../../adapters/src/status.js';
2
3
  import { parseArgs } from 'node:util';
3
4
  import { createInterface } from 'node:readline/promises';
4
5
  import { fileURLToPath } from 'node:url';
5
- import { join, resolve, dirname } from 'node:path';
6
- import { existsSync, readFileSync, accessSync, constants, rmSync } from 'node:fs';
6
+ import { join, resolve, dirname, basename } from 'node:path';
7
+ import { existsSync, readFileSync, accessSync, constants, } from 'node:fs';
7
8
  import { execFileSync } from 'node:child_process';
8
- import { planConnection, applyConnectionPlan, planUninstall, clients, atomicWrite, readConnectionState } from './connections.js';
9
- import { installRuntime, readInstallation, defaultHome, runtimePaths, withInstallLock, activateRuntime, packageName } from './runtime.js';
9
+ import { planConnection, applyConnectionPlan, planUninstall, clients, atomicWrite, readConnectionState, rollbackConnectionPlan, } from './connections.js';
10
+ import { installRuntime, readInstallation, defaultHome, runtimePaths, withInstallLock, activateRuntime, assertRuntimeCompatibility, packageName, } from './runtime.js';
11
+ import { planAntigravity, verifyAntigravity, defaultAntigravityConfig, } from './antigravity.js';
12
+ import { antigravityServerName } from '../../adapters/src/server-name.js';
10
13
  import { resolveRepository } from '../../adapters/src/repository.js';
11
14
  const appRoot = fileURLToPath(new URL('../../../', import.meta.url));
12
- const packageRoot = existsSync(join(appRoot, 'package.json')) ? appRoot : dirname(appRoot);
15
+ const packageRoot = existsSync(join(appRoot, 'package.json'))
16
+ ? appRoot
17
+ : dirname(appRoot);
13
18
  const metadata = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf8'));
14
19
  const version = metadata.version;
15
20
  async function confirm(text, yes = false) {
@@ -17,7 +22,10 @@ async function confirm(text, yes = false) {
17
22
  return true;
18
23
  if (!process.stdin.isTTY)
19
24
  throw new Error('Use --yes for noninteractive apply, or --preview to inspect without changes');
20
- const reader = createInterface({ input: process.stdin, output: process.stdout });
25
+ const reader = createInterface({
26
+ input: process.stdin,
27
+ output: process.stdout,
28
+ });
21
29
  try {
22
30
  return /^y(es)?$/i.test((await reader.question(text + ' [y/N] ')).trim());
23
31
  }
@@ -28,7 +36,10 @@ async function confirm(text, yes = false) {
28
36
  async function ask(prompt) {
29
37
  if (!process.stdin.isTTY)
30
38
  throw new Error('Pass --workspace and --client for noninteractive setup');
31
- const reader = createInterface({ input: process.stdin, output: process.stdout });
39
+ const reader = createInterface({
40
+ input: process.stdin,
41
+ output: process.stdout,
42
+ });
32
43
  try {
33
44
  return (await reader.question(prompt)).trim();
34
45
  }
@@ -36,58 +47,104 @@ async function ask(prompt) {
36
47
  reader.close();
37
48
  }
38
49
  }
39
- function preview(plan) {
50
+ function preview(plan, contents = true) {
40
51
  for (const [path, content] of Object.entries(plan.changes)) {
41
52
  if (path.includes('npm-connection'))
42
53
  continue;
43
54
  console.log((content === null ? 'remove ' : 'merge ') + join(plan.workspace, path));
44
- if (content !== null && content !== plan.expected[path])
55
+ if (contents && content !== null && content !== plan.expected[path])
45
56
  console.log(content);
46
57
  }
47
58
  }
48
- function applyAll(plans, commit) {
59
+ async function applyAll(plans, commit, verify = async () => { }) {
49
60
  const done = [];
50
61
  try {
51
62
  for (const plan of plans) {
52
63
  applyConnectionPlan(plan);
53
64
  done.push(plan);
54
65
  }
66
+ await verify();
55
67
  commit();
56
68
  }
57
69
  catch (error) {
58
70
  for (const plan of done.reverse())
59
- for (const [path, content] of Object.entries(plan.expected)) {
60
- // Already-written files are restored as one transaction; unrelated concurrent
61
- // edits are protected by each plan's optimistic concurrency check.
62
- const target = join(plan.workspace, path);
63
- const current = existsSync(target) ? readFileSync(target, 'utf8') : null;
64
- if (current !== plan.changes[path])
65
- continue;
66
- if (content !== null)
67
- atomicWrite(join(plan.workspace, path), content);
68
- else
69
- rmSync(join(plan.workspace, path), { force: true });
70
- }
71
+ rollbackConnectionPlan(plan);
71
72
  throw error;
72
73
  }
73
74
  }
74
- function plansFor(runtime, projects) { return projects.map(project => planConnection({ ...project, appRoot: runtime.appRoot, node: runtime.node })); }
75
+ function plansFor(runtime, projects) {
76
+ return projects.map((project) => planConnection({
77
+ ...project,
78
+ appRoot: runtime.appRoot,
79
+ node: runtime.node,
80
+ }));
81
+ }
75
82
  async function main() {
76
- const { values, positionals } = parseArgs({ allowPositionals: true, options: {
77
- workspace: { type: 'string' }, client: { type: 'string', multiple: true }, home: { type: 'string' },
78
- package: { type: 'string' }, yes: { type: 'boolean' }, preview: { type: 'boolean' },
79
- version: { type: 'boolean' }, help: { type: 'boolean' },
80
- } });
83
+ const { values, positionals } = parseArgs({
84
+ allowPositionals: true,
85
+ options: {
86
+ session: { type: 'string' },
87
+ workspace: { type: 'string' },
88
+ client: { type: 'string', multiple: true },
89
+ home: { type: 'string' },
90
+ package: { type: 'string' },
91
+ yes: { type: 'boolean' },
92
+ preview: { type: 'boolean' },
93
+ 'capture-new-sessions': { type: 'boolean' },
94
+ 'antigravity-config': { type: 'string' },
95
+ version: { type: 'boolean' },
96
+ help: { type: 'boolean' },
97
+ },
98
+ });
81
99
  if (values.version) {
82
100
  console.log(version);
83
101
  return;
84
102
  }
85
103
  const action = positionals[0];
86
104
  if (!action || values.help) {
87
- console.log('Knitt ' + version + '\nCommands: setup, doctor, update, rollback, uninstall, serve\nOptions: --workspace PATH --client codex|claude-code|antigravity|claude-desktop (repeatable)\n --preview --yes --home PATH --package TARBALL_OR_VERSION_SPEC\nSetup configures a persistent local runtime; remote/team service is configured separately.');
105
+ console.log('Knitt ' +
106
+ version +
107
+ '\nCommands: setup, doctor, update, rollback, uninstall, serve, continue\nOptions: --workspace PATH --client codex|claude-code|antigravity|claude-desktop (repeatable)\n --antigravity-config PATH --capture-new-sessions --preview --yes --home PATH --package TARBALL_OR_VERSION_SPEC\nSetup configures a persistent local runtime; remote/team service is configured separately.');
88
108
  return;
89
109
  }
90
110
  const home = resolve(values.home ?? defaultHome()), paths = runtimePaths(home);
111
+ if (action === 'continue') {
112
+ if (!values.workspace)
113
+ throw new Error('Pass --workspace for the connected project');
114
+ const common = (cwd) => execFileSync('git', ['rev-parse', '--path-format=absolute', '--git-common-dir'], { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
115
+ const expected = common(resolve(values.workspace));
116
+ const state = readInstallation(home);
117
+ const project = state?.projects.find((p) => {
118
+ try {
119
+ return common(p.workspace) === expected;
120
+ }
121
+ catch {
122
+ return false;
123
+ }
124
+ });
125
+ if (!project?.captureNewSessions)
126
+ throw new Error('This project has not opted into local conversation mode; run setup --capture-new-sessions first');
127
+ process.env.MEMORY_DB = project.database;
128
+ process.env.MEMORY_REPO = project.repoId;
129
+ process.env.MEMORY_CONVERSATION_MODE = '1';
130
+ await import('../../adapters/src/command.js');
131
+ return;
132
+ }
133
+ const initialState = readInstallation(home);
134
+ const configPath = resolve(values['antigravity-config'] ??
135
+ initialState?.antigravityConfig ??
136
+ defaultAntigravityConfig());
137
+ const globalOptions = (runtime, projects) => ({
138
+ configPath,
139
+ installationHome: home,
140
+ appRoot: runtime.appRoot,
141
+ node: runtime.node,
142
+ projects,
143
+ });
144
+ if (initialState?.antigravityConfig &&
145
+ configPath !== resolve(initialState.antigravityConfig))
146
+ throw new Error('This installation already manages another Antigravity config. Use its existing path: ' +
147
+ initialState.antigravityConfig);
91
148
  if (action === 'serve') {
92
149
  process.env.MEMORY_DB ??= join(paths.data, 'events.sqlite');
93
150
  await import('../../../apps/server/src/main.js');
@@ -106,7 +163,10 @@ async function main() {
106
163
  if (!state)
107
164
  problems.push('No persistent runtime installed. Run setup.');
108
165
  else {
109
- for (const file of [state.current.node, join(state.current.appRoot, 'apps/server/src/main.js')])
166
+ for (const file of [
167
+ state.current.node,
168
+ join(state.current.appRoot, 'apps/server/src/main.js'),
169
+ ])
110
170
  if (!existsSync(file))
111
171
  problems.push('Missing runtime path: ' + file + '. Run setup again.');
112
172
  try {
@@ -115,6 +175,18 @@ async function main() {
115
175
  catch {
116
176
  problems.push('Data directory is not writable: ' + paths.data);
117
177
  }
178
+ try {
179
+ const globalPlan = planAntigravity(globalOptions(state.current, state.projects));
180
+ const before = globalPlan.expected[basename(configPath)];
181
+ const servers = before ? (JSON.parse(before).mcpServers ?? {}) : {};
182
+ for (const project of state.projects.filter((p) => p.clients.includes('antigravity'))) {
183
+ if (!servers[antigravityServerName(project.workspace)])
184
+ problems.push('Missing Antigravity server: ' + project.workspace);
185
+ }
186
+ }
187
+ catch (error) {
188
+ problems.push(error instanceof Error ? error.message : String(error));
189
+ }
118
190
  for (const project of state.projects) {
119
191
  try {
120
192
  const managed = readConnectionState(project.workspace);
@@ -123,7 +195,8 @@ async function main() {
123
195
  for (const [path, snapshot] of Object.entries(managed.files)) {
124
196
  const current = readFileSync(join(project.workspace, path), 'utf8');
125
197
  if (current !== snapshot.after)
126
- problems.push('Configuration changed; review before setup: ' + join(project.workspace, path));
198
+ problems.push('Configuration changed; review before setup: ' +
199
+ join(project.workspace, path));
127
200
  }
128
201
  }
129
202
  catch {
@@ -131,7 +204,26 @@ async function main() {
131
204
  }
132
205
  }
133
206
  }
134
- console.log(JSON.stringify({ status: problems.length ? 'attention_required' : 'configured', runtime: state?.current.version, problems, note: 'Configuration check only; agent login/trust and live handoff are not verified.' }, null, 2));
207
+ const capture = (state?.projects ?? [])
208
+ .filter((p) => p.captureNewSessions)
209
+ .map((project) => {
210
+ const observations = project.clients
211
+ .flatMap((client) => readCaptureStatuses(join(dirname(project.database), 'queue', client === 'claude-code' ? 'claude' : client)))
212
+ .filter((s) => s.session.repoId === project.repoId);
213
+ return {
214
+ workspace: project.workspace,
215
+ captureObserved: observations.some((s) => s.lastCaptureAt !== null),
216
+ startupObserved: observations.length > 0,
217
+ sessions: observations,
218
+ };
219
+ });
220
+ console.log(JSON.stringify({
221
+ capture,
222
+ status: problems.length ? 'attention_required' : 'configured',
223
+ runtime: state?.current.version,
224
+ problems,
225
+ note: 'Configuration check only; agent login/trust and live handoff are not verified.',
226
+ }, null, 2));
135
227
  if (problems.length)
136
228
  process.exitCode = 1;
137
229
  return;
@@ -140,13 +232,38 @@ async function main() {
140
232
  throw new Error('Unknown command: ' + action);
141
233
  let project;
142
234
  if (action === 'setup') {
143
- const directory = values.workspace ?? await ask('Project folder: ');
144
- const selected = values.client ?? (await ask('Agents (comma separated: ' + clients.join(', ') + '): ')).split(',').map(v => v.trim());
235
+ const directory = values.workspace ?? (await ask('Project folder: '));
236
+ const selected = values.client ??
237
+ (await ask('Agents (comma separated: ' + clients.join(', ') + '): '))
238
+ .split(',')
239
+ .map((v) => v.trim());
145
240
  const { workspace, repoId } = resolveRepository(resolve(directory));
146
- project = { workspace, repoId, clients: selected, database: join(paths.data, 'events.sqlite') };
241
+ project = {
242
+ workspace,
243
+ repoId,
244
+ clients: selected,
245
+ database: join(paths.data, 'events.sqlite'),
246
+ ...(values['capture-new-sessions'] ? { captureNewSessions: true } : {}),
247
+ };
147
248
  // Validate and show changes before installing dependencies or writing settings.
249
+ const previousProject = readInstallation(home)?.projects.find((p) => p.workspace === workspace);
250
+ if (previousProject)
251
+ project.repoId = previousProject.repoId;
148
252
  const plan = planConnection({ ...project, appRoot });
149
253
  preview(plan);
254
+ const projects = [
255
+ ...(initialState?.projects ?? []).filter((p) => p.workspace !== project.workspace),
256
+ {
257
+ ...project,
258
+ clients: [
259
+ ...new Set([
260
+ ...(initialState?.projects.find((p) => p.workspace === project.workspace)?.clients ?? []),
261
+ ...project.clients,
262
+ ]),
263
+ ],
264
+ },
265
+ ];
266
+ preview(planAntigravity(globalOptions({ version, appRoot, node: process.execPath }, projects)), false);
150
267
  if (values.preview) {
151
268
  console.log('Preview only. No runtime or configuration installed.');
152
269
  return;
@@ -157,19 +274,28 @@ async function main() {
157
274
  if (!state)
158
275
  throw new Error('No installation found. Run setup first.');
159
276
  if (action === 'uninstall') {
160
- const target = values.workspace ? resolveRepository(resolve(values.workspace)).workspace : null;
277
+ const target = values.workspace
278
+ ? resolveRepository(resolve(values.workspace)).workspace
279
+ : null;
161
280
  if (!target)
162
281
  throw new Error('Pass --workspace to uninstall one project; memory data is always retained');
163
- if (!state.projects.some(p => p.workspace === target))
282
+ if (!state.projects.some((p) => p.workspace === target))
164
283
  throw new Error('Project is not managed by this installation');
165
284
  preview(planUninstall(target));
285
+ preview(planAntigravity({
286
+ ...globalOptions(state.current, state.projects.filter((p) => p.workspace !== target)),
287
+ removeOnly: true,
288
+ }), false);
166
289
  }
167
290
  else
168
- console.log(action + ' runtime for ' + state.projects.length + ' project(s); user memory is retained.');
291
+ console.log(action +
292
+ ' runtime for ' +
293
+ state.projects.length +
294
+ ' project(s); user memory is retained.');
169
295
  if (values.preview)
170
296
  return;
171
297
  }
172
- if (!await confirm('Apply ' + action + '?', values.yes)) {
298
+ if (!(await confirm('Apply ' + action + '?', values.yes))) {
173
299
  console.log('Cancelled. No changes applied.');
174
300
  return;
175
301
  }
@@ -178,26 +304,62 @@ async function main() {
178
304
  if (action === 'uninstall') {
179
305
  const target = resolveRepository(resolve(values.workspace)).workspace;
180
306
  const plan = planUninstall(target);
181
- applyAll([plan], () => atomicWrite(paths.state, JSON.stringify({ ...state, projects: state.projects.filter(p => p.workspace !== target) }, null, 2)));
307
+ const remaining = state.projects.filter((p) => p.workspace !== target);
308
+ await applyAll([
309
+ plan,
310
+ planAntigravity({
311
+ ...globalOptions(state.current, remaining),
312
+ removeOnly: true,
313
+ }),
314
+ ], () => atomicWrite(paths.state, JSON.stringify({ ...state, projects: remaining }, null, 2)));
182
315
  console.log('Project connectors removed where unchanged. User-edited settings and all memory retained. Runtime retained for other projects.');
183
316
  return;
184
317
  }
185
318
  if (project) {
186
- const existing = state?.projects.find(p => p.workspace === project.workspace);
187
- if (existing)
188
- project.clients = [...new Set([...existing.clients, ...project.clients])];
319
+ const existing = state?.projects.find((p) => p.workspace === project.workspace);
320
+ if (existing) {
321
+ project.repoId = existing.repoId;
322
+ project.clients = [
323
+ ...new Set([...existing.clients, ...project.clients]),
324
+ ];
325
+ if (existing.captureNewSessions)
326
+ project.captureNewSessions = true;
327
+ }
189
328
  }
190
- const projects = project ? [...(state?.projects ?? []).filter(p => p.workspace !== project.workspace), project] : state.projects;
329
+ const projects = project
330
+ ? [
331
+ ...(state?.projects ?? []).filter((p) => p.workspace !== project.workspace),
332
+ project,
333
+ ]
334
+ : state.projects;
191
335
  if (action !== 'rollback' && metadata.private && !values.package)
192
336
  throw new Error('This package is unpublished. Pass --package /absolute/path/to/the-built.tgz; registry installation is disabled until release.');
193
- const runtime = action === 'rollback' ? state?.previous : installRuntime(home, values.package ?? packageName + '@' + (action === 'update' ? (version.includes('-') ? 'next' : 'latest') : version));
337
+ const runtime = action === 'rollback'
338
+ ? state?.previous
339
+ : installRuntime(home, values.package ??
340
+ packageName +
341
+ '@' +
342
+ (action === 'update'
343
+ ? version.includes('-')
344
+ ? 'next'
345
+ : 'latest'
346
+ : version));
194
347
  if (!runtime)
195
348
  throw new Error('No previous runtime available');
349
+ assertRuntimeCompatibility(runtime, projects);
196
350
  const plans = plansFor(runtime, projects);
197
- applyAll(plans, () => activateRuntime(home, runtime, projects));
351
+ plans.push(planAntigravity(globalOptions(runtime, projects)));
352
+ await applyAll(plans, () => activateRuntime(home, runtime, projects, projects.some((p) => p.clients.includes('antigravity'))
353
+ ? configPath
354
+ : state?.antigravityConfig), () => verifyAntigravity(globalOptions(runtime, projects)));
198
355
  console.log('Installed runtime ' + runtime.version + '. Data: ' + paths.data);
199
- console.log('Restart connected agents and review MCP/hook trust prompts. Use /knit to select your conversation.');
200
- if (projects.some(p => p.clients.includes('claude-desktop')))
356
+ console.log('Restart connected agents and review MCP/hook trust prompts. ' +
357
+ (existsSync(join(runtime.appRoot, 'skills/knit/SKILL.md')) ? 'Use /continue-chat for projects with --capture-new-sessions; use /knit for other projects.' : 'Use /continue-chat with this runtime.'));
358
+ if (projects.some((p) => p.clients.includes('antigravity')))
359
+ console.log('Antigravity: project memory servers registered in ' +
360
+ configPath +
361
+ ' and tool connections verified. Restart Antigravity and open a fresh project chat.');
362
+ if (projects.some((p) => p.clients.includes('claude-desktop')))
201
363
  console.log('Claude Desktop: merge the generated project .memory/connectors/claude-desktop.json fragment into Desktop MCP settings, then restart.');
202
364
  });
203
365
  }