-
Notifications
You must be signed in to change notification settings - Fork 0
fix: preserve authored await verification in standalone runtime #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27ff10c
fix: run standalone authored bodies with native Node promise hooks
miyaontherelay fbb4dec
test: cover authored parent loss and same-root child replay
miyaontherelay 2e44424
fix: preserve parent signal interruption for authored resume
miyaontherelay b64b07b
fix: verify authored runtime receipts and parent-loss custody
miyaontherelay 529b930
Merge remote-tracking branch 'origin/main' into fix/authored-node-lif…
miyaontherelay 51fb034
fix: fail closed when authored parent watchdog exits
miyaontherelay d379cb1
fix: verify complete child journals and allow container init parent
miyaontherelay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { Worker } from 'node:worker_threads'; | ||
| import { createHash, createHmac } from 'node:crypto'; | ||
| import { readFileSync, writeSync } from 'node:fs'; | ||
| import { JournalClient } from './journal-client.js'; | ||
| import { executeAuthoredFlow } from './authored-flow-executor.js'; | ||
| import { loadPinnedAuthoredSource } from './authored-source-authority.js'; | ||
| import { assertAuthoredNodeVersion, parseAuthoredParentPid } from './authored-runtime-capability.js'; | ||
| import { AuthoredFlowExecutionError } from './authored-flow-error.js'; | ||
| import type { AuthoredRootMetadata } from './authored-root.js'; | ||
|
|
||
| let channelKey: string | undefined, sequence = 0; | ||
| // Capture writers before loading authored modules; credentials never enter env. | ||
| const writeFrame = writeSync, mac = createHmac; | ||
| const send = (message: unknown): void => { | ||
| const payload = JSON.stringify(message); | ||
| if (channelKey === undefined) { writeFrame(3, payload + '\n'); return; } | ||
| const seq = ++sequence; | ||
| writeFrame(3, JSON.stringify({ seq, payload, | ||
| mac: mac('sha256', channelKey).update(`${seq}\0${payload}`).digest('hex') }) + '\n'); | ||
| }; | ||
| const hash = (path: string): string => createHash('sha256').update(readFileSync(path)).digest('hex'); | ||
| const controller = new AbortController(); | ||
| let finished = false; | ||
| const abort = (): void => { | ||
| controller.abort(); | ||
| setTimeout(() => process.exit(1), 2000).unref(); | ||
| }; | ||
| process.on('SIGINT', abort); process.on('SIGTERM', abort); | ||
| // The parent owns the root lease. Do not continue authored effects after it dies. | ||
| process.stdin.on('end', () => { if (!finished) process.exit(1); }); | ||
| process.stdin.on('error', () => process.exit(1)); | ||
| // A separate event loop enforces parent loss even while authored JS is blocked. | ||
| // Capture the expected PID in the parent's spawn arguments, before any child work. | ||
| const parentPid = parseAuthoredParentPid(process.argv[2]); | ||
| const watchdog = new Worker(` | ||
| const {parentPort,workerData}=require('node:worker_threads'); | ||
| function check(){if(process.ppid!==workerData.parentPid)process.kill(process.pid,'SIGKILL');} | ||
| check();setInterval(check,50);parentPort.postMessage('ready'); | ||
| `, { eval: true, workerData: { parentPid } }); | ||
| // Enforcement must not silently disappear after the readiness Promise settles. | ||
| const watchdogLost = (): void => { if (!finished) process.kill(process.pid, 'SIGKILL'); }; | ||
| watchdog.on('error', watchdogLost); | ||
| watchdog.on('exit', watchdogLost); | ||
| let client: JournalClient | undefined; | ||
| try { | ||
| assertAuthoredNodeVersion(); | ||
| await new Promise<void>((resolve,reject)=>{watchdog.once('message',()=>resolve());watchdog.once('error',reject);}); | ||
| send({ type: 'ready', runtime: { kind: 'node', version: process.versions.node, | ||
| executableSha256: hash(process.execPath), payloadSha256: hash(process.argv[1]!) } }); | ||
| const request = await new Promise<{ channelKey: string; metadata: AuthoredRootMetadata; socketPath: string; | ||
| rootRunId: string; dataDir: string; localAgentStream?: string }>((resolve, reject) => { | ||
| let buffer = ''; | ||
| process.stdin.setEncoding('utf8'); | ||
| const onData = (chunk: string): void => { | ||
| buffer += chunk; | ||
| if (Buffer.byteLength(buffer) > 2 * 1024 * 1024) { reject(new Error('authored runtime request exceeded limit')); return; } | ||
| const end = buffer.indexOf('\n'); | ||
| if (end < 0) return; | ||
| process.stdin.off('data', onData); | ||
| try { resolve(JSON.parse(buffer.slice(0, end))); } catch { reject(new Error('invalid authored runtime request')); } | ||
| }; | ||
| process.stdin.on('data', onData); | ||
| }); | ||
| if (typeof request.channelKey !== 'string' || !/^[a-f0-9]{64}$/.test(request.channelKey)) throw new Error('invalid authored channel key'); | ||
| channelKey = request.channelKey; | ||
| controller.signal.throwIfAborted(); | ||
| const loaded = await loadPinnedAuthoredSource(request.metadata, true); | ||
| if (request.localAgentStream !== request.metadata.localAgentStream) throw new Error('authored root local agent surface mismatch'); | ||
| client = new JournalClient(request.socketPath); | ||
| await client.connect(); await client.hello('flows-authored-node'); | ||
| const result = await executeAuthoredFlow(loaded.handle, client, | ||
| request.metadata.inputPresent ? request.metadata.input : undefined, { | ||
| getDefinition: loaded.getDefinition, dataDir: request.dataDir, | ||
| flowPath: request.metadata.flowPath, rootRunId: request.rootRunId, | ||
| localAgentStream: request.localAgentStream, signal: controller.signal, | ||
| onProgress: event => send({ type: 'progress', event }), | ||
| onWait: event => send({ type: 'wait', event }), | ||
| }); | ||
| send({ type: 'result', result }); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : 'authored body failed'; | ||
| const prefix = error instanceof AuthoredFlowExecutionError ? `${error.code}: ` : ''; | ||
| send({ type: 'error', message: prefix && message.startsWith(prefix) ? message.slice(prefix.length) : message, | ||
| ...(error instanceof AuthoredFlowExecutionError ? { code: error.code, | ||
| completionReason: error.completionReason, runId: error.runId } : {}) }); | ||
| process.exitCode = 1; | ||
| } finally { | ||
| finished = true; await watchdog.terminate(); client?.close(); process.stdin.destroy(); | ||
| process.off('SIGINT', abort); process.off('SIGTERM', abort); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.