@h1r-ai/croche 0.5.3 → 0.5.4

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.
@@ -46,27 +46,35 @@ export class CrocheAgent implements AgentToolControls {
46
46
  private closing?: Promise<void>;
47
47
  private ended = false;
48
48
  private hub!: StreamHub<AgentEvent>;
49
- private handle!: WasmAgent;
49
+ private closedInfo?: {id: string; instance: string | null; created: boolean; name: string | null;
50
+ mode: string | null; visibleTools: string | undefined};
51
+ private handleValue!: WasmAgent;
52
+ private get handle(): WasmAgent {
53
+ if (this.host.disposed) throw new CrocheError('closed', 'The agent is closed.');
54
+ return this.handleValue;
55
+ }
50
56
  private host!: CrocheRuntime;
51
57
  private constructor() {}
52
58
  private initialize(handle: WasmAgent, host: CrocheRuntime) {
53
- this.handle = handle;
59
+ this.handleValue = handle;
54
60
  this.host = host;
61
+ // Port callbacks can outlive SDK close: look up the checked owner at dispatch, not the
62
+ // borrowed raw handle that close has freed. Teardown below is already fenced by Core.
55
63
  const port: CoreStreamPort<AgentEvent> = {
56
64
  observe: (onEvent, onEnd) => {
57
- call(() => handle.observe((event: string) => onEvent(JSON.parse(event) as RequestObservation),
65
+ call(() => this.handle.observe((event: string) => onEvent(JSON.parse(event) as RequestObservation),
58
66
  (error: string) => onEnd(JSON.parse(error))));
59
- return {close: () => call(() => handle.unobserve())};
67
+ return {close: () => { if (!host.closed) call(() => handle.unobserve()); }};
60
68
  },
61
69
  select: (options, onEvent) => {
62
- const id = call(() => handle.select(json(options), (event: string) => onEvent(JSON.parse(event))));
63
- return {close: () => call(() => handle.closeProjection(id)),
64
- history: options => held(() => handle.history(id, options.before, options.limit))};
70
+ const id = call(() => this.handle.select(json(options), (event: string) => onEvent(JSON.parse(event))));
71
+ return {close: () => { if (!host.closed) call(() => handle.closeProjection(id)); },
72
+ history: options => held(() => this.handle.history(id, options.before, options.limit))};
65
73
  },
66
74
  send: (input, options) => host[receiptSend](input, options),
67
- cancel: id => held(() => id === undefined ? handle.cancelTurn() : handle.cancel(id)),
75
+ cancel: id => held(() => id === undefined ? this.handle.cancelTurn() : this.handle.cancel(id)),
68
76
  readOutcome: async (id, from, captured) => JSON.parse(await held(() =>
69
- handle.readOutcome(id, from, (event: string) => captured(JSON.parse(event))))),
77
+ this.handle.readOutcome(id, from, (event: string) => captured(JSON.parse(event))))),
70
78
  };
71
79
  this.hub = new StreamHub(port);
72
80
  }
@@ -75,14 +83,14 @@ export class CrocheAgent implements AgentToolControls {
75
83
  agent.initialize(handle, host);
76
84
  return agent;
77
85
  }; }
78
- get id(): string { return call(() => this.handle.agentId()); }
79
- get instance(): string | null { return call(() => this.handle.instance()) ?? null; }
80
- get created(): boolean { return call(() => this.handle.created()); }
81
- get name(): string | null { return call(() => this.handle.name()) ?? null; }
82
- get mode(): string | null { return call(() => this.handle.mode()) ?? null; }
86
+ get id(): string { return this.closedInfo ? this.closedInfo.id : call(() => this.handle.agentId()); }
87
+ get instance(): string | null { return this.closedInfo ? this.closedInfo.instance : call(() => this.handle.instance()) ?? null; }
88
+ get created(): boolean { return this.closedInfo ? this.closedInfo.created : call(() => this.handle.created()); }
89
+ get name(): string | null { return this.closedInfo ? this.closedInfo.name : call(() => this.handle.name()) ?? null; }
90
+ get mode(): string | null { return this.closedInfo ? this.closedInfo.mode : call(() => this.handle.mode()) ?? null; }
83
91
  get visibleTools(): string[] | null {
84
92
  return call(() => {
85
- const names = this.handle.visibleTools();
93
+ const names = this.closedInfo ? this.closedInfo.visibleTools : this.handle.visibleTools();
86
94
  return names == null ? null : JSON.parse(names) as string[];
87
95
  });
88
96
  }
@@ -92,7 +100,7 @@ export class CrocheAgent implements AgentToolControls {
92
100
  setMode(mode: string | null): Promise<void> {
93
101
  return held(() => this.handle.setMode(mode ?? undefined));
94
102
  }
95
- get closed(): boolean { return call(() => this.handle.isClosed()); }
103
+ get closed(): boolean { return this.ended || this.host.closed; }
96
104
  get logId(): string { return call(() => this.handle.logId()); }
97
105
  sync(): Promise<void> { return this.host.sync(); }
98
106
  send(input: SendInput, options: Pick<SendOptions, 'signal' | 'responseFormat' | 'imageResolution'> = {}) {
@@ -127,7 +135,15 @@ export class CrocheAgent implements AgentToolControls {
127
135
  close(): Promise<void> {
128
136
  return this.closing ??= (async () => {
129
137
  this.ended = true;
130
- try { await this.host.close(); } finally { this.hub.close(); }
138
+ try {
139
+ // Preserve the small getters that Core permits after close, not its Session. Close
140
+ // joins Core's one drain; host.close then releases this exported owner once.
141
+ await held(() => this.handleValue.close());
142
+ this.closedInfo = {id: this.handleValue.agentId(), instance: this.handleValue.instance() ?? null,
143
+ created: this.handleValue.created(), name: this.handleValue.name() ?? null,
144
+ mode: this.handleValue.mode() ?? null, visibleTools: this.handleValue.visibleTools()};
145
+ await this.host.close();
146
+ } finally { this.hub.close(); }
131
147
  })();
132
148
  }
133
149
  }
@@ -136,7 +152,13 @@ export class CrocheAgent implements AgentToolControls {
136
152
  export class Croche {
137
153
  private readonly agents = new Map<string, CrocheAgent>();
138
154
  private closing?: Promise<void>;
139
- private handle!: WasmProject;
155
+ private closedInfo?: {user: string | null; installationId: string | null; crocheUserId: string | null; agentIds: string[]};
156
+ private handleValue?: WasmProject;
157
+ private get handle(): WasmProject {
158
+ if (!this.handleValue) throw new CrocheError('closed', 'The project is closed.');
159
+ return this.handleValue;
160
+ }
161
+ private set handle(handle: WasmProject) { this.handleValue = handle; }
140
162
  private pals!: Map<string, Pal>;
141
163
  private constructor() {}
142
164
 
@@ -197,10 +219,10 @@ export class Croche {
197
219
  await held(() => core().WasmProject.deleteStore(factory, json(deleteWire(options))) as Promise<unknown>);
198
220
  }
199
221
 
200
- get user(): string | null { return call(() => this.handle.user()) ?? null; }
201
- get installationId(): string | null { return call(() => this.handle.installationId()) ?? null; }
202
- get crocheUserId(): string | null { return call(() => this.handle.crocheUserId()) ?? null; }
203
- get agentIds(): readonly string[] { return call(() => JSON.parse(this.handle.agentIds()) as string[]); }
222
+ get user(): string | null { return this.closedInfo ? this.closedInfo.user : call(() => this.handle.user()) ?? null; }
223
+ get installationId(): string | null { return this.closedInfo ? this.closedInfo.installationId : call(() => this.handle.installationId()) ?? null; }
224
+ get crocheUserId(): string | null { return this.closedInfo ? this.closedInfo.crocheUserId : call(() => this.handle.crocheUserId()) ?? null; }
225
+ get agentIds(): readonly string[] { return this.closedInfo ? [...this.closedInfo.agentIds] : call(() => JSON.parse(this.handle.agentIds()) as string[]); }
204
226
 
205
227
  async agent(id?: string, options: AgentOptions = {}): Promise<CrocheAgent> {
206
228
  // Core prunes its closed slots on open; release the corresponding JS handles and hosts too.
@@ -225,7 +247,15 @@ export class Croche {
225
247
  close(): Promise<void> {
226
248
  return this.closing ??= (async () => {
227
249
  try { await held(() => this.handle.close()); }
228
- finally { await Promise.all([...this.agents.values()].map(agent => agent.close())); }
250
+ finally {
251
+ await Promise.all([...this.agents.values()].map(agent => agent.close()));
252
+ }
253
+ this.closedInfo = {user: this.handle.user() ?? null, installationId: this.handle.installationId() ?? null,
254
+ crocheUserId: this.handle.crocheUserId() ?? null, agentIds: JSON.parse(this.handle.agentIds()) as string[]};
255
+ this.handleValue?.free();
256
+ this.handleValue = undefined;
257
+ this.agents.clear();
258
+ this.pals.clear();
229
259
  })();
230
260
  }
231
261
  }