@tangle-network/agent-interface 0.48.0 → 0.49.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.
@@ -0,0 +1,162 @@
1
+ import { z } from "zod";
2
+ import { boundedIdentifierSchema, boundedStringSchema } from "./contract-limits.js";
3
+ const TERMINAL_MAX_DIMENSION = 10_000;
4
+ const terminalDimensionSchema = z.number().int().positive().max(TERMINAL_MAX_DIMENSION);
5
+ /**
6
+ * Provider-neutral handle for one interactive terminal.
7
+ *
8
+ * The handle carries no raw process id and no environment map, so a terminal
9
+ * reference never leaks host process detail or injected secrets. Every terminal
10
+ * is bound to a parent execution and carries an explicit expiry, so an orphan
11
+ * or an expired terminal fails closed at the call sites that read it.
12
+ */
13
+ export const TerminalSessionRefSchema = z.strictObject({
14
+ terminalSessionId: boundedIdentifierSchema,
15
+ parentExecutionId: boundedIdentifierSchema,
16
+ name: boundedIdentifierSchema,
17
+ shell: boundedStringSchema.min(1),
18
+ command: boundedStringSchema.optional(),
19
+ cwd: boundedStringSchema.min(1),
20
+ cols: terminalDimensionSchema,
21
+ rows: terminalDimensionSchema,
22
+ connectionId: boundedIdentifierSchema.optional(),
23
+ createdAt: z.iso.datetime().max(64),
24
+ lastActivityAt: z.iso.datetime().max(64),
25
+ expiresAt: z.iso.datetime().max(64),
26
+ isRunning: z.boolean(),
27
+ exitCode: z.number().int().optional(),
28
+ exitSignal: boundedIdentifierSchema.optional(),
29
+ attachCount: z.number().int().nonnegative(),
30
+ });
31
+ /** Raw bytes written to the terminal, normalized to UTF-8 text. */
32
+ export const TerminalInputSchema = z.strictObject({
33
+ data: boundedStringSchema,
34
+ });
35
+ /** New terminal geometry. */
36
+ export const TerminalResizeSchema = z.strictObject({
37
+ cols: terminalDimensionSchema,
38
+ rows: terminalDimensionSchema,
39
+ });
40
+ /**
41
+ * Create-or-reattach request. A present `terminalSessionId` reattaches to an
42
+ * existing terminal; its absence opens a new one under the parent execution.
43
+ * `mode` selects an `attach` to a live process or a `logical` resume that
44
+ * replays retained output.
45
+ */
46
+ export const TerminalAttachRequestSchema = z.strictObject({
47
+ parentExecutionId: boundedIdentifierSchema,
48
+ terminalSessionId: boundedIdentifierSchema.optional(),
49
+ connectionId: boundedIdentifierSchema.optional(),
50
+ mode: z.enum(["attach", "logical"]),
51
+ cols: terminalDimensionSchema.optional(),
52
+ rows: terminalDimensionSchema.optional(),
53
+ command: boundedStringSchema.optional(),
54
+ cwd: boundedStringSchema.optional(),
55
+ });
56
+ /** Result of a create-or-reattach request. */
57
+ export const TerminalAttachResultSchema = z.discriminatedUnion("status", [
58
+ z.strictObject({
59
+ status: z.enum(["attached", "reattached"]),
60
+ mode: z.enum(["attach", "logical"]),
61
+ ref: TerminalSessionRefSchema,
62
+ attachCount: z.number().int().nonnegative(),
63
+ }),
64
+ z.strictObject({
65
+ status: z.literal("unavailable"),
66
+ reason: boundedStringSchema.min(1),
67
+ }),
68
+ z.strictObject({
69
+ status: z.literal("unknown"),
70
+ message: boundedStringSchema.min(1),
71
+ retryable: z.boolean(),
72
+ }),
73
+ ]);
74
+ /**
75
+ * One ordered terminal output frame. `output` frames carry a monotonic `seq`
76
+ * that a consumer replays from, so a reconnect resumes without loss or
77
+ * duplication.
78
+ */
79
+ export const TerminalOutputEventSchema = z.discriminatedUnion("type", [
80
+ z.strictObject({
81
+ type: z.literal("ready"),
82
+ cols: terminalDimensionSchema.optional(),
83
+ rows: terminalDimensionSchema.optional(),
84
+ }),
85
+ z.strictObject({
86
+ type: z.literal("output"),
87
+ seq: z.number().int().nonnegative(),
88
+ data: boundedStringSchema,
89
+ }),
90
+ z.strictObject({
91
+ type: z.literal("resize"),
92
+ cols: terminalDimensionSchema,
93
+ rows: terminalDimensionSchema,
94
+ }),
95
+ z.strictObject({
96
+ type: z.literal("exit"),
97
+ exitCode: z.number().int().optional(),
98
+ exitSignal: boundedIdentifierSchema.optional(),
99
+ }),
100
+ z.strictObject({
101
+ type: z.literal("error"),
102
+ message: boundedStringSchema.min(1),
103
+ }),
104
+ ]);
105
+ /** Acknowledgement of a detach or close. */
106
+ export const TerminalDetachAckSchema = z.discriminatedUnion("status", [
107
+ z.strictObject({
108
+ status: z.literal("detached"),
109
+ terminalSessionId: boundedIdentifierSchema,
110
+ connectionId: boundedIdentifierSchema.optional(),
111
+ }),
112
+ z.strictObject({
113
+ status: z.literal("closed"),
114
+ terminalSessionId: boundedIdentifierSchema,
115
+ exitCode: z.number().int().optional(),
116
+ exitSignal: boundedIdentifierSchema.optional(),
117
+ }),
118
+ z.strictObject({
119
+ status: z.literal("unknown"),
120
+ terminalSessionId: boundedIdentifierSchema,
121
+ message: boundedStringSchema.min(1),
122
+ retryable: z.boolean(),
123
+ }),
124
+ ]);
125
+ /**
126
+ * Fail-closed usability check for a terminal reference. Returns true only when
127
+ * the reference parses, is running, and its expiry is in the future. A parse
128
+ * failure, a stopped terminal, or a past expiry denies use.
129
+ */
130
+ export function terminalSessionUsable(ref, nowIso) {
131
+ const parsed = TerminalSessionRefSchema.safeParse(ref);
132
+ if (!parsed.success)
133
+ return false;
134
+ const now = Date.parse(nowIso);
135
+ const expiresAt = Date.parse(parsed.data.expiresAt);
136
+ if (!Number.isFinite(now) || !Number.isFinite(expiresAt))
137
+ return false;
138
+ if (now >= expiresAt)
139
+ return false;
140
+ return parsed.data.isRunning;
141
+ }
142
+ /** Bind an attach result to the parent execution and terminal it targeted. */
143
+ export function terminalAttachResultMatchesRequest(request, result) {
144
+ const parsedRequest = TerminalAttachRequestSchema.safeParse(request);
145
+ const parsedResult = TerminalAttachResultSchema.safeParse(result);
146
+ if (!parsedRequest.success || !parsedResult.success)
147
+ return false;
148
+ const outcome = parsedResult.data;
149
+ if (outcome.status !== "attached" && outcome.status !== "reattached") {
150
+ return false;
151
+ }
152
+ if (outcome.mode !== parsedRequest.data.mode)
153
+ return false;
154
+ if (outcome.ref.parentExecutionId !== parsedRequest.data.parentExecutionId) {
155
+ return false;
156
+ }
157
+ if (parsedRequest.data.terminalSessionId !== undefined &&
158
+ outcome.ref.terminalSessionId !== parsedRequest.data.terminalSessionId) {
159
+ return false;
160
+ }
161
+ return true;
162
+ }
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export * from "./host-services.js";
8
8
  export * from "./mcp.js";
9
9
  export * from "./provider-adapter.js";
10
10
  export type * from "./environment-provider.js";
11
- export { AgentEnvironmentCapabilitiesSchema, AgentNativeContextContinuationResultSchema, AgentTurnInputSchema, AgentTurnResultSchema, agentNativeContextContinuationResultMatchesRequest, } from "./environment-provider.js";
11
+ export { AgentEnvironmentCapabilitiesSchema, AgentNativeContextContinuationResultSchema, AgentTurnInputSchema, AgentTurnResultSchema, agentNativeContextContinuationResultMatchesRequest, AccountUsageSchema, AgentEnvironmentObservationSchema, AgentEnvironmentStatusSchema, ComputeBillingSchema, EnvironmentLifecycleSchema, ModelUsageSchema, ObservationProvenanceSchema, ObservationStateSchema, PlacementDescriptorSchema, ProviderIdentitySchema, ResourceProfileSchema, ResourceUseSampleSchema, SafeEndpointSchema, agentEnvironmentObservationIdentityMatches, assertObservationCredentialFree, observationContainsCredential, observationOf, TerminalAttachRequestSchema, TerminalAttachResultSchema, TerminalDetachAckSchema, TerminalInputSchema, TerminalOutputEventSchema, TerminalResizeSchema, TerminalSessionRefSchema, terminalAttachResultMatchesRequest, terminalSessionUsable, } from "./environment-provider.js";
12
12
  export * from "./plan.js";
13
13
  export * from "./runtime-control.js";
14
14
  export * from "./portable-context.js";
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ export * from "./provider-config.js";
7
7
  export * from "./host-services.js";
8
8
  export * from "./mcp.js";
9
9
  export * from "./provider-adapter.js";
10
- export { AgentEnvironmentCapabilitiesSchema, AgentNativeContextContinuationResultSchema, AgentTurnInputSchema, AgentTurnResultSchema, agentNativeContextContinuationResultMatchesRequest, } from "./environment-provider.js";
10
+ export { AgentEnvironmentCapabilitiesSchema, AgentNativeContextContinuationResultSchema, AgentTurnInputSchema, AgentTurnResultSchema, agentNativeContextContinuationResultMatchesRequest, AccountUsageSchema, AgentEnvironmentObservationSchema, AgentEnvironmentStatusSchema, ComputeBillingSchema, EnvironmentLifecycleSchema, ModelUsageSchema, ObservationProvenanceSchema, ObservationStateSchema, PlacementDescriptorSchema, ProviderIdentitySchema, ResourceProfileSchema, ResourceUseSampleSchema, SafeEndpointSchema, agentEnvironmentObservationIdentityMatches, assertObservationCredentialFree, observationContainsCredential, observationOf, TerminalAttachRequestSchema, TerminalAttachResultSchema, TerminalDetachAckSchema, TerminalInputSchema, TerminalOutputEventSchema, TerminalResizeSchema, TerminalSessionRefSchema, terminalAttachResultMatchesRequest, terminalSessionUsable, } from "./environment-provider.js";
11
11
  export * from "./plan.js";
12
12
  export * from "./runtime-control.js";
13
13
  export * from "./portable-context.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/agent-interface",
3
- "version": "0.48.0",
3
+ "version": "0.49.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "license": "MIT",