@astrosheep/keiyaku 4.1.4 → 4.2.0

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.
@@ -1,7 +1,10 @@
1
1
  import { AkumaHandle } from "./akuma-handle.js";
2
- import type { AkumaCallContext, AkumaCallInput, AkumaConfiguration, AkumaList, AkumaListInput } from "./akuma.js";
2
+ import type { AkumaCallContext, AkumaCallInput, AkumaConfiguration, AkumaList, AkumaListInput, ResultRoute } from "./akuma.js";
3
3
  import { CALL_WITH_CONTEXT } from "./akuma-product-symbols.js";
4
4
  import type { WorldRoot } from "../world.js";
5
+ type AkumaCallLaunchInput = AkumaCallInput & Readonly<{
6
+ resultRoute?: ResultRoute;
7
+ }>;
5
8
  export declare class Akuma {
6
9
  private readonly path;
7
10
  private readonly configuration;
@@ -11,8 +14,9 @@ export declare class Akuma {
11
14
  id: string;
12
15
  }>): AkumaHandle;
13
16
  listArchetypes(): Promise<readonly string[]>;
14
- call(input: AkumaCallInput): Promise<AkumaHandle>;
15
- [CALL_WITH_CONTEXT](input: AkumaCallInput, context: AkumaCallContext): Promise<AkumaHandle>;
17
+ call(input: AkumaCallLaunchInput): Promise<AkumaHandle>;
18
+ [CALL_WITH_CONTEXT](input: AkumaCallLaunchInput, context: AkumaCallContext): Promise<AkumaHandle>;
16
19
  list(input?: AkumaListInput): Promise<AkumaList>;
17
20
  }
18
- export declare function callAkumaWithContext(akuma: Akuma, input: AkumaCallInput, context: AkumaCallContext): Promise<AkumaHandle>;
21
+ export declare function callAkumaWithContext(akuma: Akuma, input: AkumaCallLaunchInput, context: AkumaCallContext): Promise<AkumaHandle>;
22
+ export {};
@@ -102,6 +102,7 @@ export class Akuma {
102
102
  origin: { kind: "direct" },
103
103
  },
104
104
  initialBody: input.body,
105
+ ...(input.resultRoute === undefined ? {} : { resultRoute: input.resultRoute }),
105
106
  }),
106
107
  });
107
108
  return new AkumaHandle(published.id, this.path, {
@@ -57,6 +57,8 @@ export type AkumaCallInput = Readonly<{
57
57
  readonly?: true;
58
58
  allowed?: readonly AllowedAction[];
59
59
  }>;
60
+ /** Opaque, serializable caller result address carried across the Body process. */
61
+ export type ResultRoute = Readonly<unknown>;
60
62
  export type AkumaCallContext = Readonly<{
61
63
  initiatorCwd?: string;
62
64
  cwdCanonical?: true;
@@ -1,6 +1,7 @@
1
1
  import { type SessionFact, type Soul } from "./heart/index.js";
2
2
  import type { AkumaPaths } from "./identity.js";
3
3
  import type { ProviderAdapter } from "./provider.js";
4
+ import type { ResultRoute } from "./akuma.js";
4
5
  import { type UpstreamExecutionPort, type RequestChildLaunch } from "./request-serve.js";
5
6
  import { type OwnedProcess, type RunLogReference } from "../runtime/proc/run.js";
6
7
  export declare const LEASH_HELD_EXIT = 75;
@@ -11,6 +12,7 @@ export type BodyLaunch = Readonly<{
11
12
  birthSession?: Omit<SessionFact, "sequence">;
12
13
  initialBody?: string;
13
14
  refuseIfHeld?: boolean;
15
+ resultRoute?: ResultRoute;
14
16
  }>;
15
17
  export type TellWake = Readonly<{
16
18
  kind: "told";
@@ -43,6 +45,10 @@ type BodyRuntime = Readonly<{
43
45
  now(): string;
44
46
  spawnChild?(launch: RequestChildLaunch): Promise<void>;
45
47
  spawnBody?(launch: BodyLaunch): Promise<OwnedProcess>;
48
+ express?(route: ResultRoute, options: Readonly<{
49
+ as: string;
50
+ body: string;
51
+ }>): Promise<unknown>;
46
52
  upstream?: UpstreamExecutionPort;
47
53
  }>;
48
54
  export declare function bodyProcessInput(launch: BodyLaunch, bodyModuleUrl?: string): Promise<{
@@ -1,3 +1,4 @@
1
+ import { appendFile } from "node:fs/promises";
1
2
  import { fileURLToPath } from "node:url";
2
3
  import { abortableDelay } from "./abort.js";
3
4
  import { BodySupervisor } from "./body-supervisor.js";
@@ -58,24 +59,52 @@ async function launchCwd(launch) {
58
59
  return (await turnRecipe(launch.paths, soul)).cwd;
59
60
  }
60
61
  async function persistTurn(paths, turnSequence, result, completedAt) {
61
- await endTurn(paths, {
62
- turnSequence,
63
- outcome: result.kind === "answered" && result.session !== undefined
64
- ? {
62
+ if (result.kind === "answered" && result.session !== undefined) {
63
+ await endTurn(paths, {
64
+ turnSequence,
65
+ outcome: {
65
66
  kind: "answered",
66
67
  session: result.session,
67
68
  answer: result.answer,
68
69
  ...(result.historyId === undefined ? {} : { historyId: result.historyId }),
69
- }
70
- : result.kind === "answered"
71
- ? { kind: "failed", diagnostic: "Provider answered without a resumable session" }
72
- : { kind: "failed", diagnostic: result.diagnostic },
73
- completedAt,
74
- });
75
- return result.kind === "answered" && result.session === undefined ? "failed" : result.kind;
70
+ },
71
+ completedAt,
72
+ });
73
+ return { outcome: "answered", answer: result.answer };
74
+ }
75
+ const diagnostic = result.kind === "answered" ? "Provider answered without a resumable session" : result.diagnostic;
76
+ await endTurn(paths, { turnSequence, outcome: { kind: "failed", diagnostic }, completedAt });
77
+ return { outcome: "failed", diagnostic };
78
+ }
79
+ function boundedDiagnostic(error) {
80
+ const text = diagnostic(error);
81
+ return text.length <= 500 ? text : `${text.slice(0, 500)}...`;
82
+ }
83
+ async function expressInitialOutcome(launch, soul, outcome, runtime) {
84
+ if (launch.resultRoute === undefined)
85
+ return;
86
+ try {
87
+ const express = runtime.express ?? (await import("@astrosheep/square")).express;
88
+ await express(launch.resultRoute, {
89
+ as: "keiyaku",
90
+ body: JSON.stringify({ akuma: soul.id, ...outcome }),
91
+ });
92
+ }
93
+ catch (error) {
94
+ try {
95
+ await appendFile(launch.paths.log, `result-route express failed: ${boundedDiagnostic(error)}\n`);
96
+ }
97
+ catch {
98
+ /* express loss never changes Heart truth */
99
+ }
100
+ }
76
101
  }
77
102
  function defaultRuntime() {
78
- return { now: () => new Date().toISOString(), spawnChild: handoffAkumaBody, spawnBody: spawnAkumaBody };
103
+ return {
104
+ now: () => new Date().toISOString(),
105
+ spawnChild: handoffAkumaBody,
106
+ spawnBody: spawnAkumaBody,
107
+ };
79
108
  }
80
109
  export async function bodyProcessInput(launch, bodyModuleUrl = import.meta.url) {
81
110
  const encoded = Buffer.from(JSON.stringify(launch), "utf8").toString("base64url");
@@ -84,11 +113,10 @@ export async function bodyProcessInput(launch, bodyModuleUrl = import.meta.url)
84
113
  throw new Error("Akuma wake has no born soul");
85
114
  const source = bodyModuleUrl.endsWith(".ts");
86
115
  const entry = fileURLToPath(new URL(source ? "../akuma-body.ts" : "../akuma-body.js", bodyModuleUrl));
87
- const argv = source
88
- ? [process.execPath, "--import", import.meta.resolve("tsx"), entry, encoded]
89
- : [process.execPath, entry, encoded];
90
116
  return {
91
- argv,
117
+ argv: source
118
+ ? [process.execPath, "--import", import.meta.resolve("tsx"), entry, encoded]
119
+ : [process.execPath, entry, encoded],
92
120
  cwd: await launchCwd(launch),
93
121
  env: { ...process.env, KEIYAKU_ACTOR_ID: actorId },
94
122
  log: launch.paths.log,
@@ -170,8 +198,14 @@ async function runBodyTurns(input) {
170
198
  await breakBody(launch.paths, { sequence: bodySequence, end: "put-down", at: runtime.now() });
171
199
  return;
172
200
  }
173
- if (result.kind === "resume-unsupported" ||
174
- (await persistTurn(launch.paths, result.turnSequence, result, runtime.now())) === "failed") {
201
+ if (result.kind === "resume-unsupported") {
202
+ await breakBody(launch.paths, { sequence: bodySequence, end: "broke-off", at: runtime.now() });
203
+ return;
204
+ }
205
+ const outcome = await persistTurn(launch.paths, result.turnSequence, result, runtime.now());
206
+ if (initial !== undefined)
207
+ await expressInitialOutcome(launch, soul, outcome, runtime);
208
+ if (outcome.outcome === "failed") {
175
209
  await breakBody(launch.paths, { sequence: bodySequence, end: "broke-off", at: runtime.now() });
176
210
  return;
177
211
  }
@@ -1,7 +1,7 @@
1
1
  import { type AkuId } from "./identity.js";
2
2
  import type { WorldRoot } from "../world.js";
3
3
  export { Akuma, AkumaHandle, AkumaNotBornError, defaultWaitComplete } from "./akuma.js";
4
- export type { AkumaCallInput } from "./akuma.js";
4
+ export type { AkumaCallInput, ResultRoute } from "./akuma.js";
5
5
  export { ALLOWED_ACTIONS } from "./allowed.js";
6
6
  export type { AllowedAction, AllowedActions } from "./allowed.js";
7
7
  export type { WorldRoot } from "../world.js";
@@ -1,4 +1,5 @@
1
1
  import { type AkuId, type ActivityHistory } from "../../akuma/index.js";
2
+ import type { ResultRoute } from "../../akuma/akuma.js";
2
3
  import { Keiyaku, type AkumaKillResult, type AkumaHistoryResult, type AkumaObservation, type AkumaTellResult, type AkumaWaitResult, type CallResult, type ForkResult, type Keiyaku as KeiyakuContract, type Repo } from "../../index.js";
3
4
  import type { Settings } from "../../settings.js";
4
5
  import type { WorldRoot } from "../../world.js";
@@ -72,6 +73,7 @@ type InvokeInput = Readonly<{
72
73
  settings?: Settings;
73
74
  contract?: KeiyakuContract;
74
75
  repo?: Repo;
76
+ resultRoute?: ResultRoute;
75
77
  readStdin(): Promise<string>;
76
78
  }>;
77
79
  export declare function invokeAkuma(command: InvokedAkumaCommand, input: InvokeInput): Promise<AkumaInvocationResult>;
@@ -113,6 +113,7 @@ export async function invokeAkuma(command, input) {
113
113
  ...(input.contract === undefined ? {} : { contract: input.contract }),
114
114
  ...(command.alias === undefined ? {} : { alias: command.alias }),
115
115
  ...(command.allowed === undefined ? {} : { allowed: command.allowed }),
116
+ ...(input.resultRoute === undefined ? {} : { resultRoute: input.resultRoute }),
116
117
  });
117
118
  return { kind: "akuma", action: "call", result, world: input.path };
118
119
  }
@@ -1,3 +1,4 @@
1
+ import { captureRoute } from "@astrosheep/square";
1
2
  import { resolveActor } from "./actor.js";
2
3
  import { isParsedAkumaCommand } from "./commands/akuma.js";
3
4
  import { invokeContractMutation } from "./commands/contract-invoke.js";
@@ -105,12 +106,14 @@ async function invokeAkumaFromEdge(parsed, input) {
105
106
  const contract = parsed.command === "call" && parsed.contract !== undefined
106
107
  ? (await import("./selectors.js")).contractFromInput(repo, parsed.contract).contract
107
108
  : undefined;
109
+ const resultRoute = parsed.command === "call" ? captureRoute({ cwd: input.invocationCwd, env: edge.environment }) : null;
108
110
  return await invokeAkuma(parsed, {
109
111
  path,
110
112
  ...(statedCwd === undefined ? {} : { statedCwd }),
111
113
  ...(home === undefined ? {} : { home }),
112
114
  ...(configuration === undefined ? {} : { settings: configuration }),
113
115
  ...(contract === undefined ? (repo === undefined ? {} : { repo }) : { contract }),
116
+ ...(resultRoute === null ? {} : { resultRoute }),
114
117
  readStdin: edge.readStdin,
115
118
  });
116
119
  }
@@ -325,6 +328,7 @@ async function invokeParsed(invocation, runtime) {
325
328
  located: world,
326
329
  candidate: candidateWorld,
327
330
  establish: coordinates.establishWorld,
331
+ invocationCwd: cwd,
328
332
  ...(cwdSource === "input" ? { statedCwd: cwd } : {}),
329
333
  ...(repo === undefined ? {} : { repo }),
330
334
  ...(home === undefined ? {} : { home }),
@@ -1,5 +1,5 @@
1
1
  import { type AliasBinding } from "../alias/index.js";
2
- import { type AkumaStatus, type ForkReceipt, type ReadonlyRestraint } from "../akuma/akuma.js";
2
+ import { type AkumaStatus, type ForkReceipt, type ReadonlyRestraint, type ResultRoute } from "../akuma/akuma.js";
3
3
  import type { AkuId } from "../akuma/identity.js";
4
4
  import { type Dispatch, type DispatchFailure } from "../dispatch/index.js";
5
5
  import { type AkumaAlias } from "../identity/selector.js";
@@ -48,9 +48,11 @@ export type CallInput = Readonly<{
48
48
  contract?: Keiyaku;
49
49
  alias?: AkumaAlias;
50
50
  allowed?: readonly AllowedAction[];
51
+ resultRoute?: ResultRoute;
51
52
  }>;
52
53
  export type CallObservation = Readonly<{
53
54
  kind: "detached";
55
+ resultRoute: "captured" | "not-captured";
54
56
  }> | Readonly<{
55
57
  kind: "observed";
56
58
  status: AkumaStatus;
@@ -77,9 +77,10 @@ function callTimeout(value, mode) {
77
77
  }
78
78
  return value;
79
79
  }
80
- async function observeCall(handle, mode, timeoutMs) {
81
- if (mode === "detach")
82
- return { kind: "detached" };
80
+ async function observeCall(handle, mode, timeoutMs, resultRoute) {
81
+ if (mode === "detach") {
82
+ return { kind: "detached", resultRoute: resultRoute === undefined ? "not-captured" : "captured" };
83
+ }
83
84
  try {
84
85
  return { kind: "observed", status: await handle.wait(undefined, { timeoutMs }) };
85
86
  }
@@ -179,6 +180,7 @@ export async function callKeiyaku(input) {
179
180
  "contract",
180
181
  "alias",
181
182
  "allowed",
183
+ "resultRoute",
182
184
  ], "Keiyaku.call input");
183
185
  const path = nonblank(values.path, "path");
184
186
  const archetype = nonblank(values.archetype, "archetype");
@@ -191,6 +193,7 @@ export async function callKeiyaku(input) {
191
193
  const settings = settingsOption(values.settings);
192
194
  const alias = values.alias === undefined ? undefined : parseAkumaAlias(nonblank(values.alias, "alias"));
193
195
  const seat = values.contract === undefined ? undefined : seatForKeiyaku(values.contract);
196
+ const resultRoute = values.resultRoute;
194
197
  const execution = await resolveCallExecution({
195
198
  path,
196
199
  ...(cwd === undefined ? {} : { cwd }),
@@ -203,6 +206,7 @@ export async function callKeiyaku(input) {
203
206
  ...(readonlyRequested === undefined ? {} : { readonly: readonlyRequested }),
204
207
  ...(values.allowed === undefined ? {} : { allowed: values.allowed }),
205
208
  ...(execution === undefined ? {} : { cwd: execution.cwd }),
209
+ ...(resultRoute === undefined ? {} : { resultRoute }),
206
210
  };
207
211
  const handle = execution === undefined ? await world.call(call) : await callAkumaWithContext(world, call, { cwdCanonical: true });
208
212
  const completedExecution = execution ?? akumaCallExecution(handle);
@@ -226,7 +230,7 @@ export async function callKeiyaku(input) {
226
230
  }
227
231
  }
228
232
  }
229
- const observation = await observeCall(handle, mode, timeoutMs);
233
+ const observation = await observeCall(handle, mode, timeoutMs, resultRoute);
230
234
  return {
231
235
  kind: "called",
232
236
  akuma: handle.id,
@@ -7,7 +7,7 @@ export function platformLaunchPolicy(intent, platform = process.platform) {
7
7
  return { kind: "windows-launcher" };
8
8
  return {
9
9
  kind: "spawn",
10
- detached: platform !== "win32",
10
+ detached: true,
11
11
  windowsHide: true,
12
12
  shell: false,
13
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrosheep/keiyaku",
3
- "version": "4.1.4",
3
+ "version": "4.2.0",
4
4
  "files": [
5
5
  "build"
6
6
  ],
@@ -59,6 +59,7 @@
59
59
  "dependencies": {
60
60
  "@agentclientprotocol/sdk": "^1.3.0",
61
61
  "@anthropic-ai/claude-agent-sdk": "^0.3.226",
62
+ "@astrosheep/square": "0.3.13",
62
63
  "@earendil-works/pi-coding-agent": "0.80.10",
63
64
  "@opencode-ai/sdk": "1.18.3",
64
65
  "cross-spawn": "^7.0.6",