-
Notifications
You must be signed in to change notification settings - Fork 13
feat(agent-bff): audit reads and action executions #1885
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
Open
nbouliol
wants to merge
8
commits into
main
Choose a base branch
from
feature/prd-1150-bff-activity-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,564
−61
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6bf1182
feat(agent-bff): audit reads and action executions
nbouliol e9cdeca
fix(agent): drain the BFF activity logs when the embedded agent stops
nbouliol ac4ca04
fix(agent-bff): answer a Forest server outage as retryable, and audit…
nbouliol 5e29a75
fix(agent-bff): answer the review on the audit path
nbouliol 36587ae
fix(agent-bff): answer the second review round on the audit path
nbouliol 8d74201
fix(agent-bff): bound the invalidation window map and free the retry …
nbouliol b37ccb3
fix(agent-bff): answer the third review round on the audit path
nbouliol a77f003
fix(agent-bff): retry transient activity log status transitions
Tonours 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
73 changes: 73 additions & 0 deletions
73
packages/agent-bff/src/activity-log/activity-log-drainer.ts
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,73 @@ | ||
| interface InFlightOperation { | ||
| promise: Promise<unknown>; | ||
| /** What the drain names when a deadline leaves this one unfinished. Carries no record payload. */ | ||
| description: string; | ||
| } | ||
|
|
||
| /** | ||
| * Holds the audited requests and the status transitions they fire without `await`. Nothing else | ||
| * keeps the transitions alive: `server.close()` waits for connections, and one sent after the | ||
| * response is attached to none — without this, every deploy would leave entries stuck in `pending`. | ||
| * | ||
| * The requests are tracked too, and not only their transitions, for the embedded deployment: there | ||
| * the host owns the connections, so `stop()` returns while requests are still running and their | ||
| * transitions are not registered yet. | ||
| */ | ||
| export default class ActivityLogDrainer { | ||
| private readonly inFlight = new Set<InFlightOperation>(); | ||
|
|
||
| track<T>(operation: () => Promise<T>, description: string): Promise<T> { | ||
| const promise = operation(); | ||
| const entry: InFlightOperation = { promise, description }; | ||
| this.inFlight.add(entry); | ||
| promise.finally(() => this.inFlight.delete(entry)).catch(() => {}); | ||
|
|
||
| return promise; | ||
| } | ||
|
|
||
| /** | ||
| * Loops rather than settling one snapshot: a transition is registered only once the request it | ||
| * audits has finished, so a single pass would return before the work that outlives it. | ||
| * | ||
| * `timeoutMs` is the shutdown deadline the caller shares: a stalled audit store would otherwise | ||
| * hold the process past the grace its orchestrator gives it, and be SIGKILLed mid-drain. Returns | ||
| * what the deadline left unfinished, empty when everything settled. | ||
| */ | ||
| async drain(timeoutMs?: number): Promise<string[]> { | ||
| const deadline = timeoutMs === undefined ? undefined : Date.now() + timeoutMs; | ||
|
|
||
| while (this.inFlight.size > 0) { | ||
| const remainingMs = deadline === undefined ? undefined : deadline - Date.now(); | ||
|
|
||
| if (remainingMs !== undefined && remainingMs <= 0) break; | ||
|
|
||
| // eslint-disable-next-line no-await-in-loop | ||
| await this.settle(remainingMs); | ||
| } | ||
|
|
||
| return [...this.inFlight].map(entry => entry.description); | ||
| } | ||
|
|
||
| private async settle(timeoutMs?: number): Promise<void> { | ||
| const settled = Promise.allSettled([...this.inFlight].map(entry => entry.promise)); | ||
|
|
||
| if (timeoutMs === undefined) { | ||
| await settled; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let timer: NodeJS.Timeout | undefined; | ||
|
|
||
| try { | ||
| await Promise.race([ | ||
| settled, | ||
| new Promise<void>(resolve => { | ||
| timer = setTimeout(resolve, timeoutMs); | ||
| }), | ||
| ]); | ||
| } finally { | ||
| clearTimeout(timer); | ||
| } | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
packages/agent-bff/src/activity-log/activity-log-writer.ts
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,54 @@ | ||
| import type { ActivityLogContext, BffActivityLogAction } from './activity-logs-creator'; | ||
| import type { ActivityLogsWriter } from './activity-logs-service'; | ||
| import type { Logger } from '../ports/logger-port'; | ||
| import type { Context } from 'koa'; | ||
|
|
||
| import ActivityLogDrainer from './activity-log-drainer'; | ||
| import withActivityLog from './with-activity-log'; | ||
|
|
||
| export interface RecordActivityLogOptions<T> { | ||
| ctx: Context; | ||
| action: BffActivityLogAction; | ||
| context?: ActivityLogContext; | ||
| operation: () => Promise<T>; | ||
| isCompletedDespite?: (error: unknown) => boolean; | ||
| } | ||
|
|
||
| export interface ActivityLogWriter { | ||
| record<T>(options: RecordActivityLogOptions<T>): Promise<T>; | ||
| /** | ||
| * Waits for the audited requests still running and for the status transitions no connection | ||
| * holds. Called when the server stops, which shares its deadline through `timeoutMs`; returns | ||
| * the operations that deadline left unfinished. | ||
| */ | ||
| drain(timeoutMs?: number): Promise<string[]>; | ||
| } | ||
|
|
||
| export interface ActivityLogWriterOptions { | ||
| service: ActivityLogsWriter; | ||
| logger: Logger; | ||
| } | ||
|
|
||
| function describeRequest(action: BffActivityLogAction, collectionName?: string): string { | ||
| return collectionName ? `'${action}' request on '${collectionName}'` : `'${action}' request`; | ||
| } | ||
|
|
||
| export default function createActivityLogWriter({ | ||
| service, | ||
| logger, | ||
| }: ActivityLogWriterOptions): ActivityLogWriter { | ||
| const drainer = new ActivityLogDrainer(); | ||
|
|
||
| return { | ||
| record<T>(options: RecordActivityLogOptions<T>): Promise<T> { | ||
| return drainer.track( | ||
| () => withActivityLog({ ...options, service, drainer, logger }), | ||
| describeRequest(options.action, options.context?.collectionName), | ||
| ); | ||
| }, | ||
|
|
||
| drain(timeoutMs?: number): Promise<string[]> { | ||
| return drainer.drain(timeoutMs); | ||
| }, | ||
| }; | ||
| } |
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.