@agimon-ai/po-git 0.0.0-alpha.4 → 0.0.0-alpha.41

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.
@@ -2,15 +2,16 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
- const require_worktreeOperations = require("../worktreeOperations-DGUbUnYU.cjs");
5
+ const require_worktreeOperations = require("../worktreeOperations-CU0iV2h-.cjs");
6
6
  let node_fs = require("node:fs");
7
7
  node_fs = require_worktreeOperations.__toESM(node_fs, 1);
8
- let node_os = require("node:os");
9
- node_os = require_worktreeOperations.__toESM(node_os, 1);
10
8
  let node_path = require("node:path");
11
9
  node_path = require_worktreeOperations.__toESM(node_path, 1);
12
- let _agimon_ai_po_core_serverFacet = require("@agimon-ai/po-core/serverFacet");
10
+ let node_os = require("node:os");
11
+ node_os = require_worktreeOperations.__toESM(node_os, 1);
12
+ let _agimon_ai_po_core_serverPlugin = require("@agimon-ai/po-core/serverPlugin");
13
13
  let _agimon_ai_po_core_extensionFile = require("@agimon-ai/po-core/extensionFile");
14
+ let _agimon_ai_po_core_codemode = require("@agimon-ai/po-core/codemode");
14
15
  let _agimon_ai_po_session = require("@agimon-ai/po-session");
15
16
  let typebox = require("typebox");
16
17
  //#region src/schemas/runWorktreeTool.ts
@@ -254,7 +255,8 @@ async function executeRunWorktreeTool(params, operations, context, options = {})
254
255
  content: text(describe(record, context.sessionId), `Session: ${record.sessionId}`, dirtyFiles.length === 0 ? "Clean." : `${String(dirtyFiles.length)} uncommitted file(s): ${dirtyFiles.slice(0, 20).join(", ")}`),
255
256
  details: {
256
257
  action: params.action,
257
- records: [record]
258
+ records: [record],
259
+ dirtyFiles
258
260
  }
259
261
  };
260
262
  }
@@ -278,7 +280,10 @@ async function executeRunWorktreeTool(params, operations, context, options = {})
278
280
  const waiting = await operations.messages(context, params.id);
279
281
  return {
280
282
  content: waiting.length === 0 ? text("No messages.") : text(`${String(waiting.length)} message(s):`, ...waiting.map((entry) => `[${entry.sentAt}] ${entry.from}: ${entry.text}`)),
281
- details: { action: params.action }
283
+ details: {
284
+ action: params.action,
285
+ messages: waiting
286
+ }
282
287
  };
283
288
  }
284
289
  case WORKTREE_ACTIONS.prune: {
@@ -293,12 +298,172 @@ async function executeRunWorktreeTool(params, operations, context, options = {})
293
298
  `Untracked directories, left alone: ${plan.untracked.length === 0 ? "none" : plan.untracked.join(", ")}`,
294
299
  ...plan.keptBranches.length === 0 ? [] : [`Branches kept, they hold commits: ${plan.keptBranches.join(", ")}`]
295
300
  ]),
296
- details: { action: params.action }
301
+ details: {
302
+ action: params.action,
303
+ plan
304
+ }
297
305
  };
298
306
  }
299
307
  }
300
308
  }
301
309
  //#endregion
310
+ //#region src/services/worktreeNamespace/index.ts
311
+ const DECLARATIONS = `type WorktreeStatus = 'spawning' | 'running' | 'closing' | 'orphaned';
312
+
313
+ interface Worktree {
314
+ id: string;
315
+ branch: string;
316
+ baseRef: string;
317
+ /** Absolute path of the checkout, under ~/.po/git/worktrees. */
318
+ path: string;
319
+ repositoryRoot: string;
320
+ /** The session running inside the worktree. */
321
+ sessionId: string;
322
+ parentSessionId?: string;
323
+ status: WorktreeStatus;
324
+ createdAt: string;
325
+ /** How your session relates to it: 'parent' spawned it, 'child' runs inside it, 'other' is neither. */
326
+ relationship: 'parent' | 'child' | 'other';
327
+ }
328
+
329
+ interface WorktreeMessage {
330
+ worktreeId: string;
331
+ fromSessionId: string;
332
+ from: 'parent' | 'child';
333
+ text: string;
334
+ sentAt: string;
335
+ }
336
+
337
+ interface PrunePlan {
338
+ remove: Worktree[];
339
+ forget: Worktree[];
340
+ /** Kept because they hold uncommitted work. */
341
+ keptDirty: Worktree[];
342
+ untracked: string[];
343
+ /** Branches kept because they hold commits. */
344
+ keptBranches: string[];
345
+ }
346
+
347
+ declare const worktrees: {
348
+ /** Every worktree for this repository. */
349
+ list(): Promise<Worktree[]>;
350
+ /** One worktree and its uncommitted files. */
351
+ status(id: string): Promise<{ worktree: Worktree; dirtyFiles: string[] }>;
352
+ /**
353
+ * Create a worktree on a new branch and start a Po session in it, nested under this one. baseRef defaults to
354
+ * the current branch's remote-tracking ref, then the remote default branch; name labels it in the rail; task is
355
+ * the new session's first message. Setup can be slow, so give exec a larger timeoutMs.
356
+ */
357
+ spawn(request: { branch: string; baseRef?: string; name?: string; task?: string }): Promise<Worktree>;
358
+ /** Stop the worktree's session and delete its checkout. Refused on a dirty tree unless force is true. */
359
+ close(id: string, options?: { force?: boolean }): Promise<Worktree>;
360
+ /** Merge the worktree's branch into this checkout. Refused when this checkout is dirty. */
361
+ merge(id: string, options?: { message?: string }): Promise<Worktree>;
362
+ /** Remove orphaned worktrees. With dryRun, only return the plan. */
363
+ prune(options?: { dryRun?: boolean }): Promise<PrunePlan>;
364
+ /**
365
+ * Message the other side of a worktree. Use the record with relationship 'child' to report to your parent,
366
+ * and 'parent' to reach your own children. 'other' records are not communication edges.
367
+ */
368
+ send(id: string, message: string): Promise<void>;
369
+ /** Messages waiting for you from a worktree. */
370
+ messages(id: string): Promise<WorktreeMessage[]>;
371
+ };`;
372
+ const json = (value) => JSON.parse(JSON.stringify(value));
373
+ /** Git worktrees for code mode: the actions of `run_worktree` as methods, returning records instead of text. */
374
+ function createWorktreeNamespace(options) {
375
+ return {
376
+ name: "worktrees",
377
+ summary: "Git worktrees, each with its own Po session nested under this one: worktrees.list, status, spawn, close, merge, prune, send and messages.",
378
+ declarations: DECLARATIONS,
379
+ bind(run) {
380
+ if (run.sessionId !== options.execution.sessionId) throw new Error("Worktrees namespace belongs to another session.");
381
+ let disposed = false;
382
+ run.onDispose(() => {
383
+ disposed = true;
384
+ });
385
+ const view = (record) => ({
386
+ ...record,
387
+ relationship: record.parentSessionId === options.execution.sessionId ? "parent" : record.sessionId === options.execution.sessionId ? "child" : "other"
388
+ });
389
+ /** One `run_worktree` action through the tool's own code, so the tool and the namespace cannot drift. */
390
+ const act = async (name, action, input) => {
391
+ run.signal.throwIfAborted();
392
+ if (disposed) throw new Error("Worktrees namespace run is disposed.");
393
+ options.assertAvailable();
394
+ const params = validateParams({
395
+ action,
396
+ ...typeof input === "object" && input !== null ? input : {}
397
+ });
398
+ const context = {
399
+ cwd: options.execution.cwd,
400
+ sessionId: options.execution.sessionId
401
+ };
402
+ const started = Date.now();
403
+ try {
404
+ const result = await executeRunWorktreeTool(params, options.operations, context, { signal: run.signal });
405
+ options.assertAvailable();
406
+ run.record?.({
407
+ name: `worktrees.${name}`,
408
+ tool: RUN_WORKTREE_TOOL_NAME,
409
+ args: params,
410
+ ok: true,
411
+ ms: Date.now() - started,
412
+ result
413
+ });
414
+ return result;
415
+ } catch (error) {
416
+ run.record?.({
417
+ name: `worktrees.${name}`,
418
+ tool: RUN_WORKTREE_TOOL_NAME,
419
+ args: params,
420
+ ok: false,
421
+ ms: Date.now() - started,
422
+ error: error instanceof Error ? error.message : String(error)
423
+ });
424
+ throw error;
425
+ }
426
+ };
427
+ const record = (result) => view(result.details.records[0]);
428
+ const members = Object.create(null);
429
+ members.list = async () => json((await act("list", "list", {})).details.records.map(view));
430
+ members.status = async (id) => {
431
+ const { details } = await act("status", "status", { id });
432
+ return json({
433
+ worktree: view(details.records[0]),
434
+ dirtyFiles: details.dirtyFiles ?? []
435
+ });
436
+ };
437
+ members.spawn = async (request) => json(record(await act("spawn", "spawn_worktree", request)));
438
+ members.close = async (id, settings) => json(record(await act("close", "close_worktree", {
439
+ ...settings,
440
+ id
441
+ })));
442
+ members.merge = async (id, settings) => json(record(await act("merge", "merge", {
443
+ ...settings,
444
+ id
445
+ })));
446
+ members.prune = async (settings) => {
447
+ const { plan } = (await act("prune", "prune", settings)).details;
448
+ return json({
449
+ ...plan,
450
+ remove: plan.remove.map(view),
451
+ forget: plan.forget.map(view),
452
+ keptDirty: plan.keptDirty.map(view)
453
+ });
454
+ };
455
+ members.send = async (id, message) => {
456
+ await act("send", "send", {
457
+ id,
458
+ message
459
+ });
460
+ };
461
+ members.messages = async (id) => json((await act("messages", "messages", { id })).details.messages);
462
+ return members;
463
+ }
464
+ };
465
+ }
466
+ //#endregion
302
467
  //#region src/extensions/workspaces/sessions/(backend)/_lib/root.server.ts
303
468
  function progressResult(action, label) {
304
469
  return {
@@ -309,9 +474,9 @@ function progressResult(action, label) {
309
474
  details: { action }
310
475
  };
311
476
  }
312
- function createGitSession({ context, host: serverHost }) {
313
- if (serverHost.context.directEvents === void 0) throw new Error("Git headless facet requires the session direct event bus.");
314
- if (serverHost.context.sessionId === void 0) throw new Error("Git headless facet requires a session identity.");
477
+ function createGitSession({ context, host: serverHost, agent, signal }) {
478
+ if (serverHost.context.directEvents === void 0) throw new Error("Git server plugin requires the session direct event bus.");
479
+ if (serverHost.context.sessionId === void 0) throw new Error("Git server plugin requires a session identity.");
315
480
  const sessionId = serverHost.context.sessionId;
316
481
  const directEvents = serverHost.context.directEvents;
317
482
  const messageInbox = require_worktreeOperations.createWorktreeMessageInbox(directEvents, sessionId);
@@ -322,20 +487,12 @@ function createGitSession({ context, host: serverHost }) {
322
487
  sessionDelivery: () => (0, _agimon_ai_po_session.readPoSessionDelivery)(context)
323
488
  });
324
489
  const resources = [{
325
- when: {
326
- state: { "minor-mode": "help" },
327
- attribution: {
328
- kind: "minor",
329
- mode: "help"
330
- }
331
- },
332
- name: "po-git",
333
- kind: "context",
334
- read: () => (0, _agimon_ai_po_core_serverFacet.readPackageResource)(require("url").pathToFileURL(__filename).href, "llms.txt")
335
- }, {
336
- name: "po-use-git",
490
+ name: "codemode-how-to-git",
491
+ description: "Hand a branch of work to a separate session in its own checkout, keeping the current checkout untouched. Use for a long refactor, a risky migration, or parallel features that touch the same files; not for parallel work on one branch.",
337
492
  kind: "skill",
338
- read: () => (0, _agimon_ai_po_core_serverFacet.readPackageResource)(require("url").pathToFileURL(__filename).href, "src/prompts/po-use-git/SKILL.md")
493
+ pinned: true,
494
+ path: (0, _agimon_ai_po_core_serverPlugin.packageResourcePath)(require("url").pathToFileURL(__filename).href, "src/prompts/codemode-how-to-git/SKILL.md"),
495
+ read: () => (0, _agimon_ai_po_core_serverPlugin.readPackageResource)(require("url").pathToFileURL(__filename).href, "src/prompts/codemode-how-to-git/SKILL.md")
339
496
  }];
340
497
  const tool = {
341
498
  name: RUN_WORKTREE_TOOL_NAME,
@@ -355,17 +512,37 @@ function createGitSession({ context, host: serverHost }) {
355
512
  const changed = (_event, execution) => {
356
513
  directEvents.publish(require_worktreeOperations.GIT_SESSION_CHANGED_EVENT, execution.sessionId, { version: 1 });
357
514
  };
515
+ const hooks = [{
516
+ event: "tool_execution_end",
517
+ handle: changed
518
+ }, {
519
+ event: "agent_settled",
520
+ handle: changed
521
+ }];
522
+ if (agent) {
523
+ const namespace = createWorktreeNamespace({
524
+ operations,
525
+ execution: agent.context,
526
+ assertAvailable: () => {
527
+ if (signal?.aborted) throw new Error("Git session is unavailable.");
528
+ }
529
+ });
530
+ context.inject([_agimon_ai_po_core_codemode.PO_CODEMODE_SERVICE], (scoped) => {
531
+ const codemode = (0, _agimon_ai_po_core_codemode.readPoCodemode)(scoped);
532
+ if (!codemode) return void 0;
533
+ const registration = codemode.register("@agimon-ai/po-git", namespace);
534
+ const restriction = agent.registerToolRestriction({ excludedTools: [RUN_WORKTREE_TOOL_NAME] });
535
+ return () => {
536
+ restriction.dispose();
537
+ registration.dispose();
538
+ };
539
+ });
540
+ }
358
541
  return {
359
542
  value: {
360
543
  resources,
361
544
  tool,
362
- hooks: [{
363
- event: "tool_execution_end",
364
- handle: changed
365
- }, {
366
- event: "agent_settled",
367
- handle: changed
368
- }]
545
+ hooks
369
546
  },
370
547
  onDispose: () => messageInbox.close()
371
548
  };
@@ -1590,9 +1767,9 @@ const GIT_CHANGES_TYPE = "git_changes";
1590
1767
  * DESIGN PATTERNS:
1591
1768
  * - Apart from the worktrees channel. Worktrees are repository management;
1592
1769
  * this is the session's own code change.
1593
- * - Push, not poll. The session facet publishes a "changed" event after every
1594
- * tool call and settled turn; recounts are coalesced to one in flight plus
1595
- * one rerun, so a burst of tool calls costs at most two.
1770
+ * - Push, not poll. The session's server plugin publishes a "changed" event
1771
+ * after every tool call and settled turn; recounts are coalesced to one in
1772
+ * flight plus one rerun, so a burst of tool calls costs at most two.
1596
1773
  * - One sync command per session. A rebase and a push must never overlap.
1597
1774
  * - Auth is read fresh for each command from the session's workspace, and goes
1598
1775
  * only into that command's git child.
@@ -1860,11 +2037,15 @@ function parseCommand(payload) {
1860
2037
  /**
1861
2038
  * What the reader is told when a command fails.
1862
2039
  *
1863
- * An expected failure explains itself. Anything else is summarised, never
1864
- * echoed: a git error can carry a remote URL with a token in it.
2040
+ * An expected failure explains itself, minus the `[code]` tag and recovery line
2041
+ * written for the agent's tool result. A dirty close is said in the person's
2042
+ * terms, because its message tells the agent to pass `force`. Anything else is
2043
+ * summarised, never echoed: a git error can carry a remote URL with a token in it.
1865
2044
  */
1866
2045
  function failureText(error) {
1867
- return error instanceof require_worktreeOperations.PoGitExpectedError ? error.message : "The worktree operation failed.";
2046
+ if (!(error instanceof require_worktreeOperations.PoGitExpectedError)) return "The worktree operation failed.";
2047
+ if (error.code === "worktree_dirty") return "The worktree has uncommitted changes.";
2048
+ return error.message.replace(/^\[[a-z_]+\] /u, "").split("\nRecovery:")[0] ?? error.message;
1868
2049
  }
1869
2050
  /** Starts one direct-event subscription per managed session. */
1870
2051
  function createWorktreesChannel(options = {}) {
@@ -2035,20 +2216,8 @@ var agent_settled_server_default = (0, _agimon_ai_po_core_extensionFile.defineHo
2035
2216
  //#region src/extensions/workspaces/sessions/(backend)/hook/tool-execution-end.server.ts
2036
2217
  var tool_execution_end_server_default = (0, _agimon_ai_po_core_extensionFile.defineHook)((context) => context.root.hooks[0]);
2037
2218
  //#endregion
2038
- //#region src/extensions/workspaces/sessions/(backend)/resource/_lib/poGit.server.ts
2039
- var poGit_server_default$1 = (context) => context.root.resources[0];
2040
- //#endregion
2041
- //#region src/extensions/workspaces/sessions/(backend)/resource/poGit.server.ts
2042
- var poGit_server_default = (0, _agimon_ai_po_core_extensionFile.defineResource)(poGit_server_default$1);
2043
- //#endregion
2044
- //#region src/extensions/workspaces/sessions/(backend)/resource/_lib/poGitReadme.server.ts
2045
- var poGitReadme_server_default$1 = (context) => context.root.resources[2];
2046
- //#endregion
2047
- //#region src/extensions/workspaces/sessions/(backend)/resource/poGitReadme.server.ts
2048
- var poGitReadme_server_default = (0, _agimon_ai_po_core_extensionFile.defineResource)(poGitReadme_server_default$1);
2049
- //#endregion
2050
2219
  //#region src/extensions/workspaces/sessions/(backend)/resource/_lib/poUseGit.server.ts
2051
- var poUseGit_server_default$1 = (context) => context.root.resources[1];
2220
+ var poUseGit_server_default$1 = (context) => context.root.resources[0];
2052
2221
  //#endregion
2053
2222
  //#region src/extensions/workspaces/sessions/(backend)/resource/poUseGit.server.ts
2054
2223
  var poUseGit_server_default = (0, _agimon_ai_po_core_extensionFile.defineResource)(poUseGit_server_default$1);
@@ -2058,19 +2227,22 @@ var runWorktree_server_default = (0, _agimon_ai_po_core_extensionFile.defineServ
2058
2227
  //#endregion
2059
2228
  //#region generated/server.ts
2060
2229
  const at = (value, context) => typeof value === "function" ? value(context) : value;
2230
+ /* v8 ignore next -- generated normalization branch */
2231
+ const defined = (values) => values.filter((value) => value !== void 0);
2061
2232
  const via = (identity, value) => value === void 0 ? void 0 : {
2062
2233
  ...identity,
2063
2234
  ...value
2064
2235
  };
2065
- const facet = (0, _agimon_ai_po_core_serverFacet.defineServerPlugin)({
2236
+ const serverPlugin = (0, _agimon_ai_po_core_serverPlugin.defineServerPlugin)({
2066
2237
  name: "@agimon-ai/po-git",
2067
- providerOverrides: (0, _agimon_ai_po_core_serverFacet.composeServerProviderOverrides)(root_server_default),
2238
+ providerOverrides: (0, _agimon_ai_po_core_serverPlugin.composeServerProviderOverrides)(root_server_default),
2239
+ injects: (0, _agimon_ai_po_core_extensionFile.composeRootInjects)({ session: [root_server_default] }),
2068
2240
  global: (context) => ({
2069
2241
  get api() {
2070
2242
  return [at(route_server_default, context)];
2071
2243
  },
2072
2244
  get channels() {
2073
- return [() => via({ frameType: "git_changes" }, git_changes_server_default()), () => via({ frameType: "git_worktrees" }, git_worktrees_server_default())];
2245
+ return [() => via({ frameType: "git_changes" }, at(git_changes_server_default, context)), () => via({ frameType: "git_worktrees" }, at(git_worktrees_server_default, context))];
2074
2246
  }
2075
2247
  }),
2076
2248
  workspace: (context) => ({
@@ -2078,11 +2250,11 @@ const facet = (0, _agimon_ai_po_core_serverFacet.defineServerPlugin)({
2078
2250
  return [at(route_server_default, context)];
2079
2251
  },
2080
2252
  get channels() {
2081
- return [() => via({ frameType: "git_changes" }, git_changes_server_default()), () => via({ frameType: "git_worktrees" }, git_worktrees_server_default())];
2253
+ return defined([context.hosted("global") ? void 0 : () => via({ frameType: "git_changes" }, at(git_changes_server_default, context)), context.hosted("global") ? void 0 : () => via({ frameType: "git_worktrees" }, at(git_worktrees_server_default, context))]);
2082
2254
  }
2083
2255
  }),
2084
2256
  session: async (context) => {
2085
- const scopeSession = await root_server_default(context);
2257
+ const scopeSession = await context.scopeRoot("session", root_server_default, context);
2086
2258
  const contextSession = {
2087
2259
  ...context,
2088
2260
  root: scopeSession.value
@@ -2093,17 +2265,13 @@ const facet = (0, _agimon_ai_po_core_serverFacet.defineServerPlugin)({
2093
2265
  return [at(route_server_default, context)];
2094
2266
  },
2095
2267
  get channels() {
2096
- return [() => via({ frameType: "git_changes" }, git_changes_server_default()), () => via({ frameType: "git_worktrees" }, git_worktrees_server_default())];
2268
+ return defined([context.hosted("global") ? void 0 : () => via({ frameType: "git_changes" }, at(git_changes_server_default, context)), context.hosted("global") ? void 0 : () => via({ frameType: "git_worktrees" }, at(git_worktrees_server_default, context))]);
2097
2269
  },
2098
2270
  get hooks() {
2099
2271
  return [via({ event: "agent_settled" }, at(agent_settled_server_default, contextSession)), via({ event: "tool_execution_end" }, at(tool_execution_end_server_default, contextSession))];
2100
2272
  },
2101
2273
  get resources() {
2102
- return [
2103
- at(poGit_server_default, contextSession),
2104
- at(poGitReadme_server_default, contextSession),
2105
- at(poUseGit_server_default, contextSession)
2106
- ];
2274
+ return [at(poUseGit_server_default, contextSession)];
2107
2275
  },
2108
2276
  get tools() {
2109
2277
  return [via({ name: "run_worktree" }, at(runWorktree_server_default, contextSession))];
@@ -2116,7 +2284,7 @@ const facet = (0, _agimon_ai_po_core_serverFacet.defineServerPlugin)({
2116
2284
  }
2117
2285
  });
2118
2286
  //#endregion
2119
- exports.default = facet;
2120
- exports.facet = facet;
2287
+ exports.default = serverPlugin;
2288
+ exports.serverPlugin = serverPlugin;
2121
2289
 
2122
2290
  //# sourceMappingURL=server.cjs.map