@graftland/plugin-remote 0.3.1 → 0.3.2
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.
- package/dist/Auth.d.ts +2 -2
- package/dist/Auth.d.ts.map +1 -1
- package/dist/Auth.js.map +1 -1
- package/dist/{Bootstrap-Dr6sX7el.js → Bootstrap-H9gDsgsC.js} +2 -2
- package/dist/{Contract-DL2mHvaT.js → Contract-DsJtisvL.js} +2 -2
- package/dist/Contract.js +1 -1
- package/dist/{Errors-BMKmu5Y7.js → Errors-DYhsGZcZ.js} +3 -1
- package/dist/Errors.d.ts +7 -0
- package/dist/Errors.d.ts.map +1 -1
- package/dist/Errors.js +3 -1
- package/dist/Errors.js.map +1 -1
- package/dist/{FailureGroup-BcH0XOBi.js → FailureGroup-BgT73AaR.js} +14 -4
- package/dist/{Framing-zaYdOJnT.js → Framing-G9eSaeLb.js} +1 -1
- package/dist/{Graft-CVyueNOf.js → Graft-B71CPA2Y.js} +20 -16
- package/dist/{Packages-JNL34JpS.js → Packages-AgoCGotL.js} +23 -26
- package/dist/{Peer-BT7eAGmh.js → Peer-DXeVrGBh.js} +5 -5
- package/dist/{ProcessObservation-DEGrEMfP.js → ProcessObservation-gLgGirYv.js} +1 -1
- package/dist/{ServeLifecycle-CJM0gG5B.js → ServeLifecycle-Bd1FPJ6e.js} +1 -1
- package/dist/Transport.d.ts +2 -2
- package/dist/Transport.d.ts.map +1 -1
- package/dist/Transport.js.map +1 -1
- package/dist/bin.js +48 -47
- package/dist/bin.js.map +1 -1
- package/dist/{childRuntime-An0HsIzl.js → childRuntime-Bm3iSg_N.js} +29 -26
- package/dist/{connectionEnded-DCpFCjMX.js → connectionEnded-BwcxQ7W8.js} +4 -4
- package/dist/{entry-B89sepMU.js → entry-psOpX3Bj.js} +72 -35
- package/dist/host/auth/bearer.js +1 -1
- package/dist/host/auth/jwt.js +1 -1
- package/dist/host/auth/mtls.js +2 -2
- package/dist/host/auth/none.js +1 -1
- package/dist/host/command.js +4 -4
- package/dist/host/local.js +3 -3
- package/dist/host/unix.js +3 -3
- package/dist/host/websocket.js +3 -3
- package/dist/host/worker.js +4 -5
- package/dist/{importer-Bk6U_sn4.js → importer-Cvf88eAY.js} +5 -8
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/dist/internal/Certificates.js +4 -4
- package/dist/internal/Certificates.js.map +1 -1
- package/dist/internal/FailureGroup.js +3 -2
- package/dist/internal/FailureGroup.js.map +1 -1
- package/dist/internal/assembly.js +9 -2
- package/dist/internal/assembly.js.map +1 -1
- package/dist/internal/auth/mtls.js +1 -1
- package/dist/internal/auth/mtls.js.map +1 -1
- package/dist/internal/child.js +20 -17
- package/dist/internal/child.js.map +1 -1
- package/dist/internal/childRuntime.js +4 -4
- package/dist/internal/childRuntime.js.map +1 -1
- package/dist/internal/directTransport.d.ts +1 -1
- package/dist/internal/entry.d.ts.map +1 -1
- package/dist/internal/entry.js +1 -1
- package/dist/internal/entry.js.map +1 -1
- package/dist/internal/execution.js +40 -5
- package/dist/internal/execution.js.map +1 -1
- package/dist/internal/host/runner.js +7 -6
- package/dist/internal/host/runner.js.map +1 -1
- package/dist/internal/host.js +5 -5
- package/dist/internal/host.js.map +1 -1
- package/dist/internal/port.js +5 -5
- package/dist/internal/port.js.map +1 -1
- package/dist/internal/release.js +1 -1
- package/dist/internal/release.js.map +1 -1
- package/dist/internal/runner/Connection.d.ts +1 -1
- package/dist/internal/runner/Connection.js +4 -4
- package/dist/internal/runner/Connection.js.map +1 -1
- package/dist/internal/runner/Execution.js +3 -3
- package/dist/internal/runner/Execution.js.map +1 -1
- package/dist/internal/runner/Framing.js +1 -1
- package/dist/internal/runner/Framing.js.map +1 -1
- package/dist/internal/runner/Local.js +1 -1
- package/dist/internal/runner/Local.js.map +1 -1
- package/dist/internal/runner/Lock.js +1 -1
- package/dist/internal/runner/Lock.js.map +1 -1
- package/dist/internal/runner/Packages.d.ts +1 -1
- package/dist/internal/runner/Packages.js +22 -25
- package/dist/internal/runner/Packages.js.map +1 -1
- package/dist/internal/runner/Processes.js +7 -9
- package/dist/internal/runner/Processes.js.map +1 -1
- package/dist/internal/runner/Store.js +3 -3
- package/dist/internal/runner/Store.js.map +1 -1
- package/dist/internal/runner/Supervisor.js +46 -47
- package/dist/internal/runner/Supervisor.js.map +1 -1
- package/dist/internal/runner/accept.js +3 -3
- package/dist/internal/runner/accept.js.map +1 -1
- package/dist/internal/runner/model.js +1 -1
- package/dist/internal/runner/model.js.map +1 -1
- package/dist/internal/serve.js +1 -1
- package/dist/internal/serve.js.map +1 -1
- package/dist/internal/supervisorPlugin.js +1 -1
- package/dist/internal/watchdogMain.js +5 -5
- package/dist/internal/watchdogMain.js.map +1 -1
- package/dist/internal/websocketServer.js +3 -1
- package/dist/internal/websocketServer.js.map +1 -1
- package/dist/internal/workerMain.js +3 -3
- package/dist/{port-DSVUY8Bq.js → port--3x2zJ4D.js} +8 -8
- package/dist/{release-BY4Mlluy.js → release-DfZPuIcK.js} +1 -1
- package/dist/runner/auth/bearer.js +2 -2
- package/dist/runner/auth/jwt.js +4 -4
- package/dist/runner/auth/jwt.js.map +1 -1
- package/dist/runner/auth/mtls.js +3 -3
- package/dist/runner/auth/none.js +1 -1
- package/dist/runner/index.js +5 -5
- package/dist/runner/stdio.js +3 -3
- package/dist/runner/unix.js +3 -3
- package/dist/runner/websocket.js +4 -4
- package/dist/{socketChannel-C_ppdSzz.js → socketChannel-CJvL2DiB.js} +2 -2
- package/dist/{supervisorPlugin-CivhM7x5.js → supervisorPlugin-t6Z6PrhD.js} +72 -76
- package/dist/{token-Cc_1CTbE.js → token-CYBNNlhS.js} +1 -1
- package/dist/{tokenCredentials-CI6mMRRd.js → tokenCredentials-CbCuk-Rt.js} +3 -3
- package/dist/{websocketServer-D2Hk7wsA.js → websocketServer-NQhODlEt.js} +5 -3
- package/dist/{workerChannel-cmLc13vm.js → workerChannel-C8hhBJzA.js} +1 -1
- package/package.json +3 -3
- package/src/Auth.ts +9 -2
- package/src/Errors.ts +8 -0
- package/src/Transport.ts +6 -2
- package/src/bin.ts +28 -30
- package/src/index.ts +5 -3
- package/src/internal/Certificates.ts +10 -13
- package/src/internal/FailureGroup.ts +128 -121
- package/src/internal/assembly.ts +19 -10
- package/src/internal/auth/mtls.ts +19 -20
- package/src/internal/child.ts +248 -239
- package/src/internal/childRuntime.ts +81 -80
- package/src/internal/entry.ts +10 -11
- package/src/internal/execution.ts +69 -12
- package/src/internal/host/runner.ts +153 -153
- package/src/internal/host.ts +116 -120
- package/src/internal/port.ts +100 -103
- package/src/internal/release.ts +3 -2
- package/src/internal/runner/Connection.ts +52 -60
- package/src/internal/runner/Execution.ts +163 -164
- package/src/internal/runner/Framing.ts +9 -10
- package/src/internal/runner/Local.ts +26 -27
- package/src/internal/runner/Lock.ts +23 -25
- package/src/internal/runner/Packages.ts +67 -70
- package/src/internal/runner/Processes.ts +89 -92
- package/src/internal/runner/Store.ts +110 -114
- package/src/internal/runner/Supervisor.ts +392 -405
- package/src/internal/runner/accept.ts +76 -84
- package/src/internal/runner/model.ts +20 -21
- package/src/internal/serve.ts +15 -13
- package/src/internal/watchdogMain.ts +16 -11
- package/src/internal/websocketServer.ts +18 -3
- package/src/runner/auth/jwt.ts +10 -6
- package/dist/cleanup-CXLa1Wd9.js +0 -13
|
@@ -16,98 +16,99 @@ import { closedNormally } from "./runner/connectionEnded"
|
|
|
16
16
|
import { endpoint } from "./control"
|
|
17
17
|
|
|
18
18
|
/** Import a contract in the runner installation; importing this module performs no I/O. */
|
|
19
|
-
export const load = (specifier: string)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
})
|
|
19
|
+
export const load = Effect.fnUntraced(function* (specifier: string) {
|
|
20
|
+
const module = yield* loadModule(specifier)
|
|
21
|
+
const contract = fromModule(module)
|
|
22
|
+
if (Option.isNone(contract))
|
|
23
|
+
return yield* ContractNotFound.make({ module: specifier })
|
|
24
|
+
return contract.value
|
|
25
|
+
})
|
|
27
26
|
|
|
28
|
-
const connect = (
|
|
27
|
+
const connect = Effect.fnUntraced(function* (
|
|
29
28
|
contract: PreparedContract,
|
|
30
29
|
channel: Port.Channel,
|
|
31
30
|
session: string,
|
|
32
31
|
bootstrap: Bootstrap | undefined,
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
},
|
|
45
|
-
call: Services.endpoints(contract.inject),
|
|
46
|
-
...(bootstrap === undefined ? {} : { limits: bootstrap.limits }),
|
|
47
|
-
},
|
|
48
|
-
)
|
|
32
|
+
) {
|
|
33
|
+
const mux =
|
|
34
|
+
bootstrap === undefined ? undefined : yield* Multiplex.make(channel)
|
|
35
|
+
return yield* Port.make(mux === undefined ? channel : Wire.channel(mux, 1), {
|
|
36
|
+
session,
|
|
37
|
+
serve: {
|
|
38
|
+
...Services.endpoints(contract.provides),
|
|
39
|
+
[endpoint]: contract.control,
|
|
40
|
+
},
|
|
41
|
+
call: Services.endpoints(contract.inject),
|
|
42
|
+
...(bootstrap === undefined ? {} : { limits: bootstrap.limits }),
|
|
49
43
|
})
|
|
44
|
+
})
|
|
50
45
|
|
|
51
46
|
/** One execution unit; the executable supplies the platform and Graft services. */
|
|
52
|
-
export const run
|
|
47
|
+
export const run: (
|
|
53
48
|
descriptor: RunnerContract,
|
|
54
49
|
channel: Port.Channel,
|
|
55
|
-
session
|
|
50
|
+
session?: string,
|
|
56
51
|
bootstrap?: Bootstrap,
|
|
57
|
-
stop
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
),
|
|
86
|
-
Effect.forkScoped,
|
|
87
|
-
)
|
|
88
|
-
yield* Effect.raceFirst(
|
|
89
|
-
peer.await.pipe(
|
|
90
|
-
Effect.catchIf(
|
|
91
|
-
(error) => closedNormally(error.cause),
|
|
92
|
-
() => Effect.void,
|
|
93
|
-
),
|
|
94
|
-
Effect.mapError((error) =>
|
|
95
|
-
ExecutionError.make({
|
|
96
|
-
detail: "Child transport failed",
|
|
97
|
-
cause: error.cause,
|
|
98
|
-
}),
|
|
99
|
-
),
|
|
100
|
-
),
|
|
101
|
-
stop,
|
|
102
|
-
).pipe(
|
|
103
|
-
// Release exports before closing the instance scope, including on interruption.
|
|
104
|
-
Effect.ensuring(instance.release),
|
|
105
|
-
)
|
|
52
|
+
stop?: Effect.Effect<unknown, ExecutionError>,
|
|
53
|
+
) => Effect.Effect<void, ExecutionError, Graft.Graft> = Effect.fnUntraced(
|
|
54
|
+
function* (
|
|
55
|
+
descriptor: RunnerContract,
|
|
56
|
+
channel: Port.Channel,
|
|
57
|
+
session: string = "local",
|
|
58
|
+
bootstrap?: Bootstrap,
|
|
59
|
+
stop: Effect.Effect<unknown, ExecutionError> = Effect.never,
|
|
60
|
+
) {
|
|
61
|
+
const contract = prepare(descriptor)
|
|
62
|
+
const telemetry = yield* Child.telemetry(contract)
|
|
63
|
+
const loggers = yield* Logger.CurrentLoggers
|
|
64
|
+
yield* Effect.gen(function* () {
|
|
65
|
+
const peer = yield* connect(contract, channel, session, bootstrap)
|
|
66
|
+
const instance = yield* Child.lifecycle(
|
|
67
|
+
contract,
|
|
68
|
+
peer,
|
|
69
|
+
bootstrap?.identity ?? { id: session, generation: 1 },
|
|
70
|
+
telemetry,
|
|
71
|
+
)
|
|
72
|
+
const handlers = yield* contract.control.toHandlers({
|
|
73
|
+
Load: ({ input }) => instance.load(input),
|
|
74
|
+
Dispose: () => instance.dispose,
|
|
75
|
+
Events: () => telemetry.eventStream,
|
|
76
|
+
Logs: () => telemetry.logStream,
|
|
77
|
+
})
|
|
78
|
+
yield* RpcServer.make(contract.control, {
|
|
79
|
+
disableFatalDefects: true,
|
|
106
80
|
}).pipe(
|
|
81
|
+
Effect.provideContext(handlers),
|
|
107
82
|
Effect.provideService(
|
|
108
|
-
|
|
109
|
-
|
|
83
|
+
RpcServer.Protocol,
|
|
84
|
+
peer.serverProtocol(endpoint),
|
|
110
85
|
),
|
|
86
|
+
Effect.forkScoped,
|
|
111
87
|
)
|
|
112
|
-
|
|
113
|
-
|
|
88
|
+
yield* Effect.raceFirst(
|
|
89
|
+
peer.await.pipe(
|
|
90
|
+
Effect.catchIf(
|
|
91
|
+
(error) => closedNormally(error.cause),
|
|
92
|
+
() => Effect.void,
|
|
93
|
+
),
|
|
94
|
+
Effect.mapError((error) =>
|
|
95
|
+
ExecutionError.make({
|
|
96
|
+
detail: "Child transport failed",
|
|
97
|
+
cause: error.cause,
|
|
98
|
+
}),
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
stop,
|
|
102
|
+
).pipe(
|
|
103
|
+
// Release exports before closing the instance scope, including on interruption.
|
|
104
|
+
Effect.ensuring(instance.release),
|
|
105
|
+
)
|
|
106
|
+
}).pipe(
|
|
107
|
+
Effect.provideService(
|
|
108
|
+
Logger.CurrentLoggers,
|
|
109
|
+
new Set([...loggers, telemetry.logger]),
|
|
110
|
+
),
|
|
111
|
+
)
|
|
112
|
+
},
|
|
113
|
+
Effect.scoped,
|
|
114
|
+
)
|
package/src/internal/entry.ts
CHANGED
|
@@ -41,17 +41,16 @@ export const resolve = Effect.fnUntraced(function* (
|
|
|
41
41
|
const directory = path.resolve(Option.getOrElse(source, () => "."))
|
|
42
42
|
const resolver = yield* Option.match(existing, {
|
|
43
43
|
onSome: Effect.succeed,
|
|
44
|
-
onNone: ()
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}),
|
|
44
|
+
onNone: Effect.fnUntraced(function* () {
|
|
45
|
+
const importer = yield* Importer.make(directory)
|
|
46
|
+
return yield* ModuleResolver.make({
|
|
47
|
+
base: directory,
|
|
48
|
+
importer: (specifier) =>
|
|
49
|
+
String.startsWith("@graftland/plugin-remote/")(specifier)
|
|
50
|
+
? import(specifier)
|
|
51
|
+
: importer(specifier),
|
|
52
|
+
})
|
|
53
|
+
}),
|
|
55
54
|
})
|
|
56
55
|
const fail = yield* boundary(field)
|
|
57
56
|
const plugin = yield* resolver
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { Plugin } from "@graftland/graft"
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Array,
|
|
4
|
+
Cause,
|
|
5
|
+
Effect,
|
|
6
|
+
Fiber,
|
|
7
|
+
PlatformError,
|
|
8
|
+
Schema,
|
|
9
|
+
Scope,
|
|
10
|
+
} from "effect"
|
|
3
11
|
import { SecureRandom } from "effect/crypto"
|
|
12
|
+
import { RpcClientError } from "effect/rpc"
|
|
4
13
|
import type {
|
|
5
14
|
LocalTarget as Local,
|
|
6
15
|
WorkerTarget as Worker,
|
|
@@ -12,7 +21,12 @@ import type { Duration } from "effect"
|
|
|
12
21
|
import * as Connection from "./runner/Connection"
|
|
13
22
|
import * as Services from "./services"
|
|
14
23
|
import * as Host from "./host"
|
|
24
|
+
import * as Errors from "../Errors"
|
|
15
25
|
import { RemoteWithdrawn, RemoteError, MissingInstance } from "../Errors"
|
|
26
|
+
import * as ChildErrors from "./childErrors"
|
|
27
|
+
import * as Assembly from "./assembly"
|
|
28
|
+
import * as Model from "./runner/model"
|
|
29
|
+
import { ReleaseInterrupted } from "./release"
|
|
16
30
|
import * as Contract from "../Contract"
|
|
17
31
|
import * as Control from "./control"
|
|
18
32
|
import { prepare } from "./contract"
|
|
@@ -64,8 +78,8 @@ export const executionPlugin = <
|
|
|
64
78
|
...Array.map(contract.inject, (service) => service.key),
|
|
65
79
|
],
|
|
66
80
|
config: Schema.toEncoded(contract.config),
|
|
67
|
-
apply: (
|
|
68
|
-
|
|
81
|
+
apply: Effect.fnUntraced(
|
|
82
|
+
function* (input: Schema.Json) {
|
|
69
83
|
const prepared = prepare(contract)
|
|
70
84
|
const random = yield* SecureRandom.SecureRandom
|
|
71
85
|
const session = yield* random.uuidV4()
|
|
@@ -98,12 +112,57 @@ export const executionPlugin = <
|
|
|
98
112
|
),
|
|
99
113
|
)
|
|
100
114
|
return () => Effect.succeed(false)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
115
|
+
},
|
|
116
|
+
Effect.annotateLogs(annotations),
|
|
117
|
+
Effect.mapError((error) => publicError(contract.error, error)),
|
|
118
|
+
),
|
|
105
119
|
})
|
|
106
120
|
|
|
121
|
+
/** Failures raised by the host machinery; a permissive declared schema must not claim them. */
|
|
122
|
+
const internalErrors: ReadonlyArray<abstract new (...args: never) => unknown> =
|
|
123
|
+
[
|
|
124
|
+
Errors.ProcessExited,
|
|
125
|
+
Errors.TransportError,
|
|
126
|
+
Errors.LocalTerminationTimeout,
|
|
127
|
+
Errors.CredentialsError,
|
|
128
|
+
Errors.AuthenticationFailed,
|
|
129
|
+
Errors.RemoteEntryError,
|
|
130
|
+
ChildErrors.AlreadyLoaded,
|
|
131
|
+
ChildErrors.ContractNotFound,
|
|
132
|
+
ChildErrors.ModuleImportFailed,
|
|
133
|
+
ChildErrors.ActivationFailed,
|
|
134
|
+
Model.PackageError,
|
|
135
|
+
Model.SessionAccessDenied,
|
|
136
|
+
Model.StaleSession,
|
|
137
|
+
Model.LeaseExpired,
|
|
138
|
+
Model.SessionStopped,
|
|
139
|
+
Model.SessionConflict,
|
|
140
|
+
Model.StateStorageError,
|
|
141
|
+
Model.ExecutionError,
|
|
142
|
+
Model.TerminationUnconfirmed,
|
|
143
|
+
Model.RunnerClosing,
|
|
144
|
+
Model.StateDirectoryLocked,
|
|
145
|
+
Model.RemoteTerminationUnconfirmed,
|
|
146
|
+
ReleaseInterrupted,
|
|
147
|
+
RpcClientError.RpcClientError,
|
|
148
|
+
...Assembly.errors,
|
|
149
|
+
]
|
|
150
|
+
const isInternal = (error: unknown) =>
|
|
151
|
+
Schema.isSchemaError(error) ||
|
|
152
|
+
PlatformError.isPlatformError(error) ||
|
|
153
|
+
Array.some(internalErrors, (Class) => error instanceof Class)
|
|
154
|
+
const isPublic = (
|
|
155
|
+
error: unknown,
|
|
156
|
+
): error is
|
|
157
|
+
| Contract.UndeclaredFailure
|
|
158
|
+
| RemoteWithdrawn
|
|
159
|
+
| RemoteError
|
|
160
|
+
| MissingInstance =>
|
|
161
|
+
Schema.is(Contract.UndeclaredFailure)(error) ||
|
|
162
|
+
Schema.is(RemoteWithdrawn)(error) ||
|
|
163
|
+
Schema.is(RemoteError)(error) ||
|
|
164
|
+
Schema.is(MissingInstance)(error)
|
|
165
|
+
|
|
107
166
|
export const publicError = <E extends Schema.Top>(
|
|
108
167
|
declared: E,
|
|
109
168
|
error: unknown,
|
|
@@ -113,11 +172,9 @@ export const publicError = <E extends Schema.Top>(
|
|
|
113
172
|
| RemoteWithdrawn
|
|
114
173
|
| RemoteError
|
|
115
174
|
| MissingInstance =>
|
|
116
|
-
|
|
175
|
+
isPublic(error)
|
|
117
176
|
? error
|
|
118
|
-
:
|
|
119
|
-
|
|
120
|
-
Schema.is(RemoteError)(error) ||
|
|
121
|
-
Schema.is(MissingInstance)(error)
|
|
177
|
+
: // Classify host failures before the declared schema, which may be structural.
|
|
178
|
+
!isInternal(error) && Schema.is(declared)(error)
|
|
122
179
|
? error
|
|
123
180
|
: RemoteError.make({ cause: Cause.fail(error) })
|
|
@@ -19,169 +19,169 @@ import * as Multiplex from "../runner/Multiplex"
|
|
|
19
19
|
import * as Packages from "../runner/Packages"
|
|
20
20
|
import * as Wire from "../runner/Wire"
|
|
21
21
|
import { awaitTermination, defaults, childOf } from "../runner/model"
|
|
22
|
-
export const connect = (
|
|
22
|
+
export const connect = Effect.fnUntraced(function* (
|
|
23
23
|
target: ConfiguredRunner,
|
|
24
24
|
contractModule: string,
|
|
25
25
|
options: Port.Options,
|
|
26
26
|
resources: Scope.Scope,
|
|
27
27
|
processScope: Scope.Scope,
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
const instance = target.instance
|
|
28
|
+
) {
|
|
29
|
+
const instance = target.instance
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
31
|
+
const margin = Duration.toMillis(target.margin ?? "5 seconds")
|
|
32
|
+
const channel = yield* connectCapability(target.explicitAuth).pipe(
|
|
33
|
+
Scope.provide(processScope),
|
|
34
|
+
)
|
|
35
|
+
const mux = yield* Multiplex.make(channel).pipe(Scope.provide(resources))
|
|
36
|
+
const control = yield* Port.make(Wire.channel(mux, 0), {
|
|
37
|
+
session: "session-v1",
|
|
38
|
+
serve: {},
|
|
39
|
+
call: { session: Wire.Session },
|
|
40
|
+
}).pipe(Scope.provide(resources))
|
|
41
|
+
const session = yield* RpcClient.make(Wire.Session).pipe(
|
|
42
|
+
Effect.provideService(
|
|
43
|
+
RpcClient.Protocol,
|
|
44
|
+
control.clientProtocol("session"),
|
|
45
|
+
),
|
|
46
|
+
Scope.provide(resources),
|
|
47
|
+
)
|
|
48
|
+
const expectedGeneration = yield* session.HighWater({ instance })
|
|
49
|
+
const record = yield* session.Open({
|
|
50
|
+
instance,
|
|
51
|
+
expectedGeneration,
|
|
52
|
+
requestId: options.session,
|
|
53
|
+
limits: defaults,
|
|
54
|
+
...(target.lease === undefined
|
|
55
|
+
? {}
|
|
56
|
+
: {
|
|
57
|
+
leaseMillis: Duration.toMillis(target.lease),
|
|
58
|
+
}),
|
|
59
|
+
})
|
|
60
|
+
const failures = yield* FailureGroup.make<TransportError>().pipe(
|
|
61
|
+
Scope.provide(resources),
|
|
62
|
+
)
|
|
63
|
+
const finishStarted = yield* Ref.make(false)
|
|
64
|
+
const terminate = yield* Effect.cached(
|
|
65
|
+
Effect.gen(function* () {
|
|
66
|
+
yield* Ref.set(finishStarted, true)
|
|
67
|
+
// A failed close must not skip Dispose; both outcomes are reported together below.
|
|
68
|
+
const closed = yield* Effect.exit(failures.close)
|
|
69
|
+
const result = yield* awaitTermination(
|
|
70
|
+
record.handle,
|
|
71
|
+
session.Dispose(record.handle),
|
|
72
|
+
record.leaseMillis,
|
|
73
|
+
margin,
|
|
74
|
+
)
|
|
75
|
+
const { code, signal, stderr, cause } = result.exit
|
|
76
|
+
yield* Effect.logInfo("Remote execution terminated", {
|
|
77
|
+
code,
|
|
78
|
+
...(signal === null ? {} : { signal }),
|
|
79
|
+
...(stderr === undefined || stderr === "" ? {} : { stderr }),
|
|
80
|
+
...(cause === undefined || cause.reasons.length === 0 ? {} : { cause }),
|
|
81
|
+
}).pipe(
|
|
82
|
+
Effect.annotateLogs({
|
|
83
|
+
"remote.instance": record.handle.key.instance,
|
|
84
|
+
"remote.generation": record.handle.generation,
|
|
85
|
+
"remote.termination": result.kind,
|
|
86
|
+
}),
|
|
87
|
+
)
|
|
88
|
+
const failure = Exit.isFailure(closed)
|
|
89
|
+
? Cause.combine(closed.cause, result.cause)
|
|
90
|
+
: result.cause
|
|
91
|
+
if (failure.reasons.length > 0)
|
|
92
|
+
return yield* Effect.failCause(cleanupCause(failure))
|
|
93
|
+
}),
|
|
94
|
+
)
|
|
95
|
+
yield* Scope.addFinalizer(
|
|
96
|
+
processScope,
|
|
97
|
+
Effect.gen(function* () {
|
|
98
|
+
if (!(yield* Ref.get(finishStarted)))
|
|
99
|
+
yield* terminate.pipe(
|
|
100
|
+
Effect.catchCause((cause) => Effect.failCause(cleanupCause(cause))),
|
|
74
101
|
)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
102
|
+
}),
|
|
103
|
+
)
|
|
104
|
+
const renewed = yield* Effect.forever(
|
|
105
|
+
Effect.sleep(record.leaseMillis / 3).pipe(
|
|
106
|
+
Effect.andThen(session.Renew(record.handle)),
|
|
107
|
+
),
|
|
108
|
+
).pipe(Effect.forkIn(resources))
|
|
109
|
+
const code = yield* session.Load({
|
|
110
|
+
handle: record.handle,
|
|
111
|
+
specifier: contractModule,
|
|
112
|
+
digest: target.expect?.digest !== undefined,
|
|
113
|
+
})
|
|
114
|
+
yield* Packages.verify(code, target.expect)
|
|
115
|
+
const loaded = yield* session.Status(record.handle)
|
|
116
|
+
const peer = yield* Port.make(Wire.channel(mux, 1), {
|
|
117
|
+
...options,
|
|
118
|
+
session: record.handle.token,
|
|
119
|
+
limits: record.limits,
|
|
120
|
+
}).pipe(Scope.provide(resources))
|
|
121
|
+
// RPC forwarding cancellation is expected; resource cleanup is collected by Connection's scopes.
|
|
122
|
+
const report = (cause: Cause.Cause<unknown>) =>
|
|
123
|
+
Cause.hasInterruptsOnly(cause)
|
|
124
|
+
? Effect.void
|
|
125
|
+
: failures.failCause(
|
|
126
|
+
Cause.map(cause, (error) =>
|
|
127
|
+
TransportError.make({ cause: Cause.fail(error) }),
|
|
128
|
+
),
|
|
89
129
|
)
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}),
|
|
93
|
-
)
|
|
94
|
-
yield* Scope.addFinalizer(
|
|
95
|
-
processScope,
|
|
96
|
-
Effect.gen(function* () {
|
|
97
|
-
if (!(yield* Ref.get(finishStarted)))
|
|
98
|
-
yield* terminate.pipe(
|
|
99
|
-
Effect.catchCause((cause) => Effect.failCause(cleanupCause(cause))),
|
|
100
|
-
)
|
|
101
|
-
}),
|
|
102
|
-
)
|
|
103
|
-
const renewed = yield* Effect.forever(
|
|
104
|
-
Effect.sleep(record.leaseMillis / 3).pipe(
|
|
105
|
-
Effect.andThen(session.Renew(record.handle)),
|
|
106
|
-
),
|
|
107
|
-
).pipe(Effect.forkIn(resources))
|
|
108
|
-
const code = yield* session.Load({
|
|
109
|
-
handle: record.handle,
|
|
110
|
-
specifier: contractModule,
|
|
111
|
-
digest: target.expect?.digest !== undefined,
|
|
112
|
-
})
|
|
113
|
-
yield* Packages.verify(code, target.expect)
|
|
114
|
-
const loaded = yield* session.Status(record.handle)
|
|
115
|
-
const peer = yield* Port.make(Wire.channel(mux, 1), {
|
|
116
|
-
...options,
|
|
117
|
-
session: record.handle.token,
|
|
118
|
-
limits: record.limits,
|
|
119
|
-
}).pipe(Scope.provide(resources))
|
|
120
|
-
// RPC forwarding cancellation is expected; resource cleanup is collected by Connection's scopes.
|
|
121
|
-
const report = (cause: Cause.Cause<unknown>) =>
|
|
130
|
+
yield* failures.await.pipe(
|
|
131
|
+
Effect.catchCause((cause) =>
|
|
122
132
|
Cause.hasInterruptsOnly(cause)
|
|
123
133
|
? Effect.void
|
|
124
|
-
:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
)
|
|
129
|
-
yield* failures.await.pipe(
|
|
130
|
-
Effect.catchCause((cause) =>
|
|
131
|
-
Cause.hasInterruptsOnly(cause)
|
|
132
|
-
? Effect.void
|
|
133
|
-
: peer.fail(TransportError.make({ cause })),
|
|
134
|
-
),
|
|
135
|
-
Effect.forkIn(resources),
|
|
136
|
-
)
|
|
134
|
+
: peer.fail(TransportError.make({ cause })),
|
|
135
|
+
),
|
|
136
|
+
Effect.forkIn(resources),
|
|
137
|
+
)
|
|
137
138
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
),
|
|
139
|
+
yield* failures.run(
|
|
140
|
+
Fiber.join(renewed).pipe(
|
|
141
|
+
Effect.catchCause(
|
|
142
|
+
Effect.fnUntraced(function* (cause) {
|
|
143
|
+
if (Cause.hasInterruptsOnly(cause)) return
|
|
144
|
+
const status = yield* Effect.exit(
|
|
145
|
+
session.Status(record.handle).pipe(Effect.timeout("5 seconds")),
|
|
146
|
+
)
|
|
147
|
+
if (Exit.isSuccess(status) && status.value.phase === "Stopped") {
|
|
148
|
+
const exit = status.value.termination.exit
|
|
149
|
+
yield* report(
|
|
150
|
+
Cause.combine(
|
|
151
|
+
cause,
|
|
152
|
+
Cause.fail(
|
|
153
|
+
ProcessExited.make({
|
|
154
|
+
pid: Option.map(childOf(loaded), (child) => child.pid),
|
|
155
|
+
exitCode: exit.code,
|
|
156
|
+
stderr: exit.stderr ?? "",
|
|
157
|
+
statusCause: exit.cause ?? Cause.empty,
|
|
158
|
+
}),
|
|
159
159
|
),
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
),
|
|
169
|
-
),
|
|
170
|
-
)
|
|
171
|
-
return {
|
|
172
|
-
resources,
|
|
173
|
-
pid: Option.map(childOf(loaded), (child) => child.pid),
|
|
174
|
-
diagnostics: failures.cause,
|
|
175
|
-
terminate: () =>
|
|
176
|
-
Effect.gen(function* () {
|
|
177
|
-
// No renewal may extend a disconnected session while termination waits out its lease.
|
|
178
|
-
yield* Fiber.interrupt(renewed)
|
|
179
|
-
yield* terminate
|
|
160
|
+
),
|
|
161
|
+
)
|
|
162
|
+
} else
|
|
163
|
+
yield* report(
|
|
164
|
+
Exit.isFailure(status)
|
|
165
|
+
? Cause.combine(cause, status.cause)
|
|
166
|
+
: cause,
|
|
167
|
+
)
|
|
180
168
|
}),
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
169
|
+
),
|
|
170
|
+
),
|
|
171
|
+
)
|
|
172
|
+
return {
|
|
173
|
+
resources,
|
|
174
|
+
pid: Option.map(childOf(loaded), (child) => child.pid),
|
|
175
|
+
diagnostics: failures.cause,
|
|
176
|
+
terminate: Effect.fnUntraced(function* () {
|
|
177
|
+
// No renewal may extend a disconnected session while termination waits out its lease.
|
|
178
|
+
yield* Fiber.interrupt(renewed)
|
|
179
|
+
yield* terminate
|
|
180
|
+
}),
|
|
181
|
+
transport: {
|
|
182
|
+
...peer,
|
|
183
|
+
failure: peer.await,
|
|
184
|
+
report,
|
|
185
|
+
},
|
|
186
|
+
} satisfies Connected
|
|
187
|
+
})
|