@astrosheep/keiyaku 4.1.3 → 4.1.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.
@@ -1,7 +1,11 @@
1
+ import type { SquareRoute } from "@astrosheep/square";
1
2
  import { AkumaHandle } from "./akuma-handle.js";
2
3
  import type { AkumaCallContext, AkumaCallInput, AkumaConfiguration, AkumaList, AkumaListInput } from "./akuma.js";
3
4
  import { CALL_WITH_CONTEXT } from "./akuma-product-symbols.js";
4
5
  import type { WorldRoot } from "../world.js";
6
+ type AkumaCallLaunchInput = AkumaCallInput & Readonly<{
7
+ resultRoute?: SquareRoute;
8
+ }>;
5
9
  export declare class Akuma {
6
10
  private readonly path;
7
11
  private readonly configuration;
@@ -11,8 +15,9 @@ export declare class Akuma {
11
15
  id: string;
12
16
  }>): AkumaHandle;
13
17
  listArchetypes(): Promise<readonly string[]>;
14
- call(input: AkumaCallInput): Promise<AkumaHandle>;
15
- [CALL_WITH_CONTEXT](input: AkumaCallInput, context: AkumaCallContext): Promise<AkumaHandle>;
18
+ call(input: AkumaCallLaunchInput): Promise<AkumaHandle>;
19
+ [CALL_WITH_CONTEXT](input: AkumaCallLaunchInput, context: AkumaCallContext): Promise<AkumaHandle>;
16
20
  list(input?: AkumaListInput): Promise<AkumaList>;
17
21
  }
18
- export declare function callAkumaWithContext(akuma: Akuma, input: AkumaCallInput, context: AkumaCallContext): Promise<AkumaHandle>;
22
+ export declare function callAkumaWithContext(akuma: Akuma, input: AkumaCallLaunchInput, context: AkumaCallContext): Promise<AkumaHandle>;
23
+ 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, {
@@ -1,3 +1,4 @@
1
+ import type { SquareRoute } from "@astrosheep/square";
1
2
  import { type SessionFact, type Soul } from "./heart/index.js";
2
3
  import type { AkumaPaths } from "./identity.js";
3
4
  import type { ProviderAdapter } from "./provider.js";
@@ -11,6 +12,7 @@ export type BodyLaunch = Readonly<{
11
12
  birthSession?: Omit<SessionFact, "sequence">;
12
13
  initialBody?: string;
13
14
  refuseIfHeld?: boolean;
15
+ resultRoute?: SquareRoute;
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: SquareRoute, 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";
@@ -74,8 +75,35 @@ async function persistTurn(paths, turnSequence, result, completedAt) {
74
75
  });
75
76
  return result.kind === "answered" && result.session === undefined ? "failed" : result.kind;
76
77
  }
78
+ function boundedDiagnostic(error) {
79
+ const text = diagnostic(error);
80
+ return text.length <= 500 ? text : `${text.slice(0, 500)}...`;
81
+ }
82
+ async function expressInitialOutcome(launch, soul, outcome, runtime) {
83
+ if (launch.resultRoute === undefined)
84
+ return;
85
+ try {
86
+ const express = runtime.express ?? (await import("@astrosheep/square")).express;
87
+ await express(launch.resultRoute, {
88
+ as: "keiyaku",
89
+ body: JSON.stringify({ akuma: soul.id, outcome }),
90
+ });
91
+ }
92
+ catch (error) {
93
+ try {
94
+ await appendFile(launch.paths.log, `result-route express failed: ${boundedDiagnostic(error)}\n`);
95
+ }
96
+ catch {
97
+ /* express loss never changes Heart truth */
98
+ }
99
+ }
100
+ }
77
101
  function defaultRuntime() {
78
- return { now: () => new Date().toISOString(), spawnChild: handoffAkumaBody, spawnBody: spawnAkumaBody };
102
+ return {
103
+ now: () => new Date().toISOString(),
104
+ spawnChild: handoffAkumaBody,
105
+ spawnBody: spawnAkumaBody,
106
+ };
79
107
  }
80
108
  export async function bodyProcessInput(launch, bodyModuleUrl = import.meta.url) {
81
109
  const encoded = Buffer.from(JSON.stringify(launch), "utf8").toString("base64url");
@@ -84,11 +112,10 @@ export async function bodyProcessInput(launch, bodyModuleUrl = import.meta.url)
84
112
  throw new Error("Akuma wake has no born soul");
85
113
  const source = bodyModuleUrl.endsWith(".ts");
86
114
  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
115
  return {
91
- argv,
116
+ argv: source
117
+ ? [process.execPath, "--import", import.meta.resolve("tsx"), entry, encoded]
118
+ : [process.execPath, entry, encoded],
92
119
  cwd: await launchCwd(launch),
93
120
  env: { ...process.env, KEIYAKU_ACTOR_ID: actorId },
94
121
  log: launch.paths.log,
@@ -170,8 +197,14 @@ async function runBodyTurns(input) {
170
197
  await breakBody(launch.paths, { sequence: bodySequence, end: "put-down", at: runtime.now() });
171
198
  return;
172
199
  }
173
- if (result.kind === "resume-unsupported" ||
174
- (await persistTurn(launch.paths, result.turnSequence, result, runtime.now())) === "failed") {
200
+ if (result.kind === "resume-unsupported") {
201
+ await breakBody(launch.paths, { sequence: bodySequence, end: "broke-off", at: runtime.now() });
202
+ return;
203
+ }
204
+ const outcome = await persistTurn(launch.paths, result.turnSequence, result, runtime.now());
205
+ if (initial !== undefined)
206
+ await expressInitialOutcome(launch, soul, outcome, runtime);
207
+ if (outcome === "failed") {
175
208
  await breakBody(launch.paths, { sequence: bodySequence, end: "broke-off", at: runtime.now() });
176
209
  return;
177
210
  }
@@ -1,3 +1,4 @@
1
+ import type { SquareRoute } from "@astrosheep/square";
1
2
  import { type AkuId, type ActivityHistory } from "../../akuma/index.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";
@@ -72,6 +73,7 @@ type InvokeInput = Readonly<{
72
73
  settings?: Settings;
73
74
  contract?: KeiyakuContract;
74
75
  repo?: Repo;
76
+ resultRoute?: SquareRoute;
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,3 +1,4 @@
1
+ import type { SquareRoute } from "@astrosheep/square";
1
2
  import { type AliasBinding } from "../alias/index.js";
2
3
  import { type AkumaStatus, type ForkReceipt, type ReadonlyRestraint } from "../akuma/akuma.js";
3
4
  import type { AkuId } from "../akuma/identity.js";
@@ -48,9 +49,11 @@ export type CallInput = Readonly<{
48
49
  contract?: Keiyaku;
49
50
  alias?: AkumaAlias;
50
51
  allowed?: readonly AllowedAction[];
52
+ resultRoute?: SquareRoute;
51
53
  }>;
52
54
  export type CallObservation = Readonly<{
53
55
  kind: "detached";
56
+ resultRoute: "captured" | "not-captured";
54
57
  }> | Readonly<{
55
58
  kind: "observed";
56
59
  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
  };
@@ -128,16 +128,23 @@ async function retainDetachedExitEvidence(log, path, from, code, signal) {
128
128
  export async function spawnDetachedProcess(input) {
129
129
  const log = await open(input.log, "a");
130
130
  let launched = false;
131
+ let logClosed = false;
132
+ const closeLog = async () => {
133
+ if (logClosed)
134
+ return;
135
+ logClosed = true;
136
+ await log.close();
137
+ };
131
138
  try {
132
139
  const from = (await log.stat()).size;
133
140
  const child = spawn(input.argv[0], input.argv.slice(1), spawnOptionsFor("retained", input, ["ignore", log.fd, log.fd]));
134
- await new Promise((resolve, reject) => {
135
- child.once("spawn", resolve);
136
- child.once("error", reject);
137
- });
138
- if (child.pid === undefined)
139
- throw new Error("detached process spawned without a pid");
140
- const pid = child.pid;
141
+ let state = "active";
142
+ let termination;
143
+ const invalidate = () => {
144
+ state = "inert";
145
+ };
146
+ child.once("exit", invalidate);
147
+ child.once("close", invalidate);
141
148
  const exited = new Promise((resolve, reject) => {
142
149
  child.once("close", (code, signal) => {
143
150
  void (async () => {
@@ -151,7 +158,7 @@ export async function spawnDetachedProcess(input) {
151
158
  failure = error;
152
159
  }
153
160
  try {
154
- await log.close();
161
+ await closeLog();
155
162
  }
156
163
  catch (error) {
157
164
  failure ??= error;
@@ -164,13 +171,13 @@ export async function spawnDetachedProcess(input) {
164
171
  });
165
172
  });
166
173
  void exited.catch(() => undefined);
167
- let state = "active";
168
- let termination;
169
- const invalidate = () => {
170
- state = "inert";
171
- };
172
- child.once("exit", invalidate);
173
- child.once("close", invalidate);
174
+ await new Promise((resolve, reject) => {
175
+ child.once("spawn", resolve);
176
+ child.once("error", reject);
177
+ });
178
+ if (child.pid === undefined)
179
+ throw new Error("detached process spawned without a pid");
180
+ const pid = child.pid;
174
181
  launched = true;
175
182
  return {
176
183
  pid,
@@ -194,7 +201,7 @@ export async function spawnDetachedProcess(input) {
194
201
  }
195
202
  finally {
196
203
  if (!launched)
197
- await log.close();
204
+ await closeLog();
198
205
  }
199
206
  }
200
207
  function terminalOutcome(code, stdout, stderr, consuming) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrosheep/keiyaku",
3
- "version": "4.1.3",
3
+ "version": "4.1.5",
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": "file:../square/.keiyaku/wt/garage",
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",