GeezerVison is showing github.com — Merge pull request #2844 from developing-today-forks/code-g/14189-ses… · developing-today-forks/opencode@33e20a1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,7 @@ import type { EventLog } from "@opencode/schema/event-log" | 71 | 71 | import type { FileDiff } from "@opencode/schema/file-diff" | 72 | 72 | import { Job } from "./job.js" | 73 | 73 | import { SessionEnvironment } from "./session/environment.js" | | 74 | +import { SessionFleetContext } from "./session/fleet-context.js" | 74 | 75 | import { SessionRouteTarget } from "./session/route-target.js" | 75 | 76 | import { ModelRoute } from "./model-route.js" | 76 | 77 | import { InstructionEntry } from "./session/instruction-entry.js" | @@ -494,6 +495,11 @@ const layer = Layer.effect( | 494 | 495 | }) | 495 | 496 | } | 496 | 497 | } | | 498 | + const fleetContext = SessionFleetContext.start({ | | 499 | + sessionID, | | 500 | + parentID: input.parentID, | | 501 | + environment: SessionFleetContext.capture(), | | 502 | + }) | 497 | 503 | const projected = yield* bus | 498 | 504 | .publish( | 499 | 505 | SessionEvent.Created, | @@ -510,6 +516,7 @@ const layer = Layer.effect( | 510 | 516 | // Children inherit metadata and permissions the way they inherit | 511 | 517 | // location, so host policies that read them treat the family uniformly. | 512 | 518 | metadata: input.metadata ?? parent?.metadata, | | 519 | + fleetCtxID: fleetContext.FLEET_CTX_ID, | 513 | 520 | subagentPolicy: input.subagentPolicy, | 514 | 521 | permissions: input.permissions ?? parent?.permissions, | 515 | 522 | // A detached tree never sprouts an interactive leaf: a child cannot opt out. | @@ -633,6 +640,7 @@ const layer = Layer.effect( | 633 | 640 | // lifecycle write then fails on the deleted row. | 634 | 641 | yield* McpxSubagent.releaseSubagentSession(sessionID).pipe(Effect.catch(() => Effect.void)) | 635 | 642 | yield* environments.clear(sessionID) | | 643 | + SessionFleetContext.end(sessionID) | 636 | 644 | ModelRoute.forgetPins(sessionID) | 637 | 645 | yield* bus.publish(SessionEvent.Deleted, { sessionID }) | 638 | 646 | yield* bus.remove(sessionID) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session/execution.ts +23-5Lines changed: 23 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ import { SubagentReport } from "./execution/subagent-report.js" | 14 | 14 | import { SessionRunCoordinator } from "./run-coordinator.js" | 15 | 15 | import { SessionRunner } from "./runner/index.js" | 16 | 16 | import { SessionSchema } from "./schema.js" | | 17 | +import { SessionFleetContext } from "./fleet-context.js" | 17 | 18 | import { SessionStore } from "./store.js" | 18 | 19 | import { SessionSynthetic } from "./synthetic.js" | 19 | 20 | import { toSessionError } from "./to-session-error.js" | @@ -181,7 +182,11 @@ export const layer = Layer.effect( | 181 | 182 | // Bind before the started publication yields to SQLite so a cancel arriving during | 182 | 183 | // that window still matches this generation. | 183 | 184 | const before = yield* jobs.currentGeneration(sessionID) | 184 | | - const event = yield* bus.publish(SessionEvent.Execution.Started, { sessionID }, claimOnCommit(sessionID)) | | 185 | + const event = yield* bus.publish( | | 186 | + SessionEvent.Execution.Started, | | 187 | + { sessionID, fleetCtxID: SessionFleetContext.id(sessionID) }, | | 188 | + claimOnCommit(sessionID), | | 189 | + ) | 185 | 190 | const bound = before?.running ? before : yield* jobs.currentGeneration(sessionID) | 186 | 191 | subagentReport.started(sessionID, { | 187 | 192 | startedSeq: event.durable.seq, | @@ -199,7 +204,11 @@ export const layer = Layer.effect( | 199 | 204 | if (outcome.type === "interrupted" && outcome.reason === "shutdown") { | 200 | 205 | subagentReport.clear(sessionID) | 201 | 206 | // Shutdown keeps the claim and the running Job row for restart continuity. | 202 | | - yield* bus.publish(SessionEvent.Execution.Interrupted, { sessionID, reason: "shutdown" }) | | 207 | + yield* bus.publish(SessionEvent.Execution.Interrupted, { | | 208 | + sessionID, | | 209 | + reason: "shutdown", | | 210 | + ...SessionFleetContext.stamp(sessionID), | | 211 | + }) | 203 | 212 | return | 204 | 213 | } | 205 | 214 | // Claim the Job generation before cancelling it so the settlement commit owns its outbox row. | @@ -252,11 +261,20 @@ function publishTerminal( | 252 | 261 | outcome: ReturnType<typeof terminal>, | 253 | 262 | commit: (seq: number) => Effect.Effect<void>, | 254 | 263 | ) { | 255 | | - if (outcome.type === "succeeded") return bus.publish(SessionEvent.Execution.Succeeded, { sessionID }, { commit }) | | 264 | + if (outcome.type === "succeeded") | | 265 | + return bus.publish(SessionEvent.Execution.Succeeded, { sessionID, ...SessionFleetContext.stamp(sessionID) }, { commit }) | 256 | 266 | if (outcome.type === "interrupted") { | 257 | | - return bus.publish(SessionEvent.Execution.Interrupted, { sessionID, reason: outcome.reason }, { commit }) | | 267 | + return bus.publish( | | 268 | + SessionEvent.Execution.Interrupted, | | 269 | + { sessionID, reason: outcome.reason, ...SessionFleetContext.stamp(sessionID) }, | | 270 | + { commit }, | | 271 | + ) | 258 | 272 | } | 259 | | - return bus.publish(SessionEvent.Execution.Failed, { sessionID, error: outcome.error }, { commit }) | | 273 | + return bus.publish( | | 274 | + SessionEvent.Execution.Failed, | | 275 | + { sessionID, error: outcome.error, ...SessionFleetContext.stamp(sessionID) }, | | 276 | + { commit }, | | 277 | + ) | 260 | 278 | } | 261 | 279 |
| 262 | 280 | export const node = makeGlobalNode({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session/execution/restart.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,7 @@ import { WriteTool } from "../../tool/plugin/write.js" | 24 | 24 | import { VideoGenerateTool } from "../../tool/plugin/video-generate.js" | 25 | 25 | import { VideoGeneration } from "../../video-generation.js" | 26 | 26 | import { SessionEvent } from "../event.js" | | 27 | +import { SessionFleetContext } from "../fleet-context.js" | 27 | 28 | import { SessionExecution } from "../execution.js" | 28 | 29 | import { SessionHistory } from "../history.js" | 29 | 30 | import { SessionSchema } from "../schema.js" | @@ -114,7 +115,7 @@ export const layer = (options?: Options) => | 114 | 115 | // counter atomically with the terminal event. | 115 | 116 | yield* bus.publish( | 116 | 117 | SessionEvent.Execution.Failed, | 117 | | - { sessionID, error: RESUME_EXHAUSTED }, | | 118 | + { sessionID, error: RESUME_EXHAUSTED, ...SessionFleetContext.stamp(sessionID) }, | 118 | 119 | { commit: () => store.release(sessionID) }, | 119 | 120 | ) | 120 | 121 | return false | @@ -334,6 +335,7 @@ export const layer = (options?: Options) => | 334 | 335 | if (!isReadonlyArrayNonEmpty(result.content)) return | 335 | 336 | yield* bus.publish(SessionEvent.Tool.Success, { | 336 | 337 | sessionID, | | 338 | + ...SessionFleetContext.stamp(sessionID), | 337 | 339 | assistantMessageID: call.assistantMessageID, | 338 | 340 | id: call.part.id, | 339 | 341 | content: result.content, | @@ -445,6 +447,7 @@ export const layer = (options?: Options) => | 445 | 447 | if (verdict.kind === "applied") { | 446 | 448 | yield* bus.publish(SessionEvent.Tool.Success, { | 447 | 449 | sessionID, | | 450 | + ...SessionFleetContext.stamp(sessionID), | 448 | 451 | assistantMessageID: call.assistantMessageID, | 449 | 452 | id: call.part.id, | 450 | 453 | content: [ | @@ -460,6 +463,7 @@ export const layer = (options?: Options) => | 460 | 463 | } | 461 | 464 | yield* bus.publish(SessionEvent.Tool.Failed, { | 462 | 465 | sessionID, | | 466 | + ...SessionFleetContext.stamp(sessionID), | 463 | 467 | assistantMessageID: call.assistantMessageID, | 464 | 468 | id: call.part.id, | 465 | 469 | error: interruptedFailure( | @@ -474,6 +478,7 @@ export const layer = (options?: Options) => | 474 | 478 | } | 475 | 479 | yield* bus.publish(SessionEvent.Tool.Failed, { | 476 | 480 | sessionID, | | 481 | + ...SessionFleetContext.stamp(sessionID), | 477 | 482 | assistantMessageID: call.assistantMessageID, | 478 | 483 | id: call.part.id, | 479 | 484 | error: interruptedFailure( | |
Collapse file Copy file name to clipboard +68Lines changed: 68 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export * as SessionFleetContext from "./fleet-context.js" | | 2 | + | | 3 | +import type { SessionSchema } from "./schema.js" | | 4 | + | | 5 | +const names = [ | | 6 | + "FLEET_CTX_ID", | | 7 | + "FLEET_CTX_ORIGIN", | | 8 | + "FLEET_CTX_HOST", | | 9 | + "FLEET_CTX_USER", | | 10 | + "FLEET_CTX_PID", | | 11 | + "FLEET_CTX_ANCESTRY", | | 12 | + "FLEET_CTX_TASK_ID", | | 13 | + "FLEET_CTX_TASK_NAME", | | 14 | + "FLEET_CTX_MUSE_SESSION", | | 15 | + "FLEET_CTX_MODEL", | | 16 | + "FLEET_CTX_OPENCODE_SESSION", | | 17 | + "FLEET_CTX_OPENCODE_PARENT", | | 18 | + "FLEET_CTX_SCHEDULED_JOB", | | 19 | + "FLEET_CTX_MESSAGE_ID", | | 20 | + "FLEET_CTX_GIT_HEADS", | | 21 | + "FLEET_CTX_INVOCATION", | | 22 | +] as const | | 23 | + | | 24 | +export type Environment = Readonly<Partial<Record<(typeof names)[number], string>>> | | 25 | + | | 26 | +const sessions = new Map<SessionSchema.ID, Environment>() | | 27 | +const safeValue = /^[A-Za-z0-9._:@/+=,~-]{1,256}$/ | | 28 | + | | 29 | +export function capture(environment: NodeJS.ProcessEnv = process.env): Environment { | | 30 | + return Object.fromEntries( | | 31 | + names.flatMap((name) => { | | 32 | + const value = environment[name] | | 33 | + return value !== undefined && safeValue.test(value) ? [[name, value]] : [] | | 34 | + }), | | 35 | + ) as Environment | | 36 | +} | | 37 | + | | 38 | +/** Snapshot the process context once, inherit it in children, and make this session the newest chain link. */ | | 39 | +export function start(input: { | | 40 | + sessionID: SessionSchema.ID | | 41 | + parentID?: SessionSchema.ID | | 42 | + environment?: Environment | | 43 | +}): Environment { | | 44 | + const parent = input.parentID ? sessions.get(input.parentID) : undefined | | 45 | + const current = parent ?? input.environment ?? capture() | | 46 | + const ancestry = current.FLEET_CTX_ANCESTRY?.split("/") ?? [] | | 47 | + const chain = { | | 48 | + ...current, | | 49 | + FLEET_CTX_OPENCODE_SESSION: input.sessionID, | | 50 | + ...(input.parentID ? { FLEET_CTX_OPENCODE_PARENT: input.parentID } : {}), | | 51 | + FLEET_CTX_ANCESTRY: [...ancestry, "opencode"].join("/"), | | 52 | + } | | 53 | + sessions.set(input.sessionID, chain) | | 54 | + return chain | | 55 | +} | | 56 | + | | 57 | +export function id(sessionID: SessionSchema.ID): string | undefined { | | 58 | + return sessions.get(sessionID)?.FLEET_CTX_ID | | 59 | +} | | 60 | + | | 61 | +export function stamp(sessionID: SessionSchema.ID) { | | 62 | + const contextID = id(sessionID) | | 63 | + return contextID === undefined ? {} : { fleetCtxID: contextID } | | 64 | +} | | 65 | + | | 66 | +export function end(sessionID: SessionSchema.ID) { | | 67 | + sessions.delete(sessionID) | | 68 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session/runner/llm.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import { InstructionState } from "../instruction-state.js" | 11 | 11 | import { SessionCompaction } from "../compaction.js" | 12 | 12 | import { SessionContext } from "../context.js" | 13 | 13 | import { SessionEvent } from "../event.js" | | 14 | +import { SessionFleetContext } from "../fleet-context.js" | 14 | 15 | import { SessionInbox } from "../inbox.js" | 15 | 16 | import { SessionBackground } from "../background.js" | 16 | 17 | import { Job } from "../../job.js" | @@ -630,6 +631,7 @@ const layer = Layer.effect( | 630 | 631 | } | 631 | 632 | yield* bus.publish(SessionEvent.Tool.Failed, { | 632 | 633 | sessionID, | | 634 | + ...SessionFleetContext.stamp(sessionID), | 633 | 635 | assistantMessageID: message.id, | 634 | 636 | id: tool.id, | 635 | 637 | error, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session/runner/publish-llm-event.ts +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import { Bus } from "../../bus.js" | 11 | 11 | import { SessionEvent } from "../event.js" | 12 | 12 | import { SessionMessage } from "../message.js" | 13 | 13 | import { SessionSchema } from "../schema.js" | | 14 | +import { SessionFleetContext } from "../fleet-context.js" | 14 | 15 | import { SessionError } from "@opencode/schema/session-error" | 15 | 16 | import { Money } from "@opencode/schema/money" | 16 | 17 | import { Text } from "../../util/text.js" | @@ -374,6 +375,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp | 374 | 375 | LiveToolMetadata.clear(assistantMessageID, id) | 375 | 376 | yield* bus.publish(SessionEvent.Tool.Failed, { | 376 | 377 | sessionID: input.sessionID, | | 378 | + ...SessionFleetContext.stamp(input.sessionID), | 377 | 379 | assistantMessageID, | 378 | 380 | id, | 379 | 381 | error: | @@ -503,6 +505,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp | 503 | 505 | tool.providerExecuted = event.providerExecuted === true | 504 | 506 | yield* bus.publish(SessionEvent.Tool.Called, { | 505 | 507 | sessionID: input.sessionID, | | 508 | + ...SessionFleetContext.stamp(input.sessionID), | 506 | 509 | assistantMessageID, | 507 | 510 | id: event.id, | 508 | 511 | input: asRecord(event.input), | @@ -529,6 +532,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp | 529 | 532 | if (event.result.type === "error") { | 530 | 533 | yield* bus.publish(SessionEvent.Tool.Failed, { | 531 | 534 | sessionID: input.sessionID, | | 535 | + ...SessionFleetContext.stamp(input.sessionID), | 532 | 536 | assistantMessageID, | 533 | 537 | id: event.id, | 534 | 538 | error: { type: "tool.execution", message: stringify(event.result.value) }, | @@ -540,6 +544,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp | 540 | 544 | } | 541 | 545 | yield* bus.publish(SessionEvent.Tool.Success, { | 542 | 546 | sessionID: input.sessionID, | | 547 | + ...SessionFleetContext.stamp(input.sessionID), | 543 | 548 | assistantMessageID, | 544 | 549 | id: event.id, | 545 | 550 | content: hostedContent(event.result), | @@ -575,6 +580,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp | 575 | 580 | LiveToolMetadata.set(assistantMessageID, id, update) | 576 | 581 | yield* bus.publish(SessionEvent.Tool.Progress, { | 577 | 582 | sessionID: input.sessionID, | | 583 | + ...SessionFleetContext.stamp(input.sessionID), | 578 | 584 | assistantMessageID, | 579 | 585 | id, | 580 | 586 | metadata: update, | @@ -594,6 +600,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp | 594 | 600 | if (!isReadonlyArrayNonEmpty(content)) return yield* Effect.die(new Error(`Tool execution has no content: ${id}`)) | 595 | 601 | yield* bus.publish(SessionEvent.Tool.Success, { | 596 | 602 | sessionID: input.sessionID, | | 603 | + ...SessionFleetContext.stamp(input.sessionID), | 597 | 604 | assistantMessageID, | 598 | 605 | id, | 599 | 606 | content, | |
Collapse file Copy file name to clipboard +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, test } from "bun:test" | | 2 | +import { SessionFleetContext } from "../src/session/fleet-context.js" | | 3 | +import { SessionSchema } from "../src/session/schema.js" | | 4 | + | | 5 | +describe("session fleet context", () => { | | 6 | + test("captures only allowlisted safe env values and represents absence", () => { | | 7 | + const captured = SessionFleetContext.capture({ | | 8 | + FLEET_CTX_ID: "task-23795", | | 9 | + FLEET_CTX_TASK_ID: "23795", | | 10 | + FLEET_CTX_MODEL: "model key is rejected", | | 11 | + API_KEY: "secret", | | 12 | + }) | | 13 | + | | 14 | + expect(captured).toEqual({ FLEET_CTX_ID: "task-23795", FLEET_CTX_TASK_ID: "23795" }) | | 15 | + expect(SessionFleetContext.capture({})).toEqual({}) | | 16 | + }) | | 17 | + | | 18 | + test("carries the id into a child and extends the session chain", () => { | | 19 | + const parentID = SessionSchema.ID.make("ses_parent") | | 20 | + const childID = SessionSchema.ID.make("ses_child") | | 21 | + SessionFleetContext.start({ | | 22 | + sessionID: parentID, | | 23 | + environment: { FLEET_CTX_ID: "task-23795", FLEET_CTX_ANCESTRY: "muse" }, | | 24 | + }) | | 25 | + | | 26 | + const child = SessionFleetContext.start({ sessionID: childID, parentID }) | | 27 | + | | 28 | + expect(SessionFleetContext.id(childID)).toBe("task-23795") | | 29 | + expect(child).toMatchObject({ | | 30 | + FLEET_CTX_ID: "task-23795", | | 31 | + FLEET_CTX_OPENCODE_SESSION: childID, | | 32 | + FLEET_CTX_OPENCODE_PARENT: parentID, | | 33 | + FLEET_CTX_ANCESTRY: "muse/opencode/opencode", | | 34 | + }) | | 35 | + }) | | 36 | + | | 37 | + test("stamps session records and clears in-memory context at session end", () => { | | 38 | + const sessionID = SessionSchema.ID.make("ses_end") | | 39 | + SessionFleetContext.start({ sessionID, environment: { FLEET_CTX_ID: "task-23795" } }) | | 40 | + expect(SessionFleetContext.stamp(sessionID)).toEqual({ fleetCtxID: "task-23795" }) | | 41 | + | | 42 | + SessionFleetContext.end(sessionID) | | 43 | + | | 44 | + expect(SessionFleetContext.stamp(sessionID)).toEqual({}) | | 45 | + }) | | 46 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/schema/src/session-event.ts +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,6 +52,8 @@ export const Created = Event.durable({ | 52 | 52 | model: Model.Ref.pipe(optional), | 53 | 53 | /** Host-supplied annotations resolved at creation, including any inherited from a parent. */ | 54 | 54 | metadata: SessionMetadata.pipe(optional), | | 55 | + /** Fleet correlation id captured from the host environment; never contains credentials. */ | | 56 | + fleetCtxID: Schema.String.pipe(optional), | 55 | 57 | subagentPolicy: SessionSubagentPolicy.pipe(optional), | 56 | 58 | permissions: Permission.Ruleset.pipe(optional), | 57 | 59 | /** Detached (non-interactive) mode, resolved at creation including inheritance from a parent. */ | @@ -266,23 +268,24 @@ export const InboxDeliveryChanged = Event.durable({ | 266 | 268 | export type InboxDeliveryChanged = typeof InboxDeliveryChanged.Type | 267 | 269 |
| 268 | 270 | export namespace Execution { | 269 | | - export const Started = Event.durable({ type: "session.execution.started", ...options, schema: Base }) | | 271 | + const ExecutionBase = { ...Base, fleetCtxID: Schema.String.pipe(optional) } | | 272 | + export const Started = Event.durable({ type: "session.execution.started", ...options, schema: ExecutionBase }) | 270 | 273 | export type Started = typeof Started.Type | 271 | 274 |
| 272 | | - export const Succeeded = Event.durable({ type: "session.execution.succeeded", ...options, schema: Base }) | | 275 | + export const Succeeded = Event.durable({ type: "session.execution.succeeded", ...options, schema: ExecutionBase }) | 273 | 276 | export type Succeeded = typeof Succeeded.Type | 274 | 277 |
| 275 | 278 | export const Failed = Event.durable({ | 276 | 279 | type: "session.execution.failed", | 277 | 280 | ...options, | 278 | | - schema: { ...Base, error: SessionError.Error }, | | 281 | + schema: { ...ExecutionBase, error: SessionError.Error }, | 279 | 282 | }) | 280 | 283 | export type Failed = typeof Failed.Type | 281 | 284 |
| 282 | 285 | export const Interrupted = Event.durable({ | 283 | 286 | type: "session.execution.interrupted", | 284 | 287 | ...options, | 285 | | - schema: { ...Base, reason: Schema.Literals(["user", "shutdown", "superseded", "inactivity"]) }, | | 288 | + schema: { ...ExecutionBase, reason: Schema.Literals(["user", "shutdown", "superseded", "inactivity"]) }, | 286 | 289 | }) | 287 | 290 | export type Interrupted = typeof Interrupted.Type | 288 | 291 | } | @@ -511,6 +514,7 @@ export namespace Tool { | 511 | 514 | ...Base, | 512 | 515 | assistantMessageID: SessionMessage.ID, | 513 | 516 | id: Schema.String, | | 517 | + fleetCtxID: Schema.String.pipe(optional), | 514 | 518 | } | 515 | 519 |
| 516 | 520 | export namespace Input { | |
|