-
Notifications
You must be signed in to change notification settings - Fork 10
fix(workflow-executor): operation activity log targets the acted record (PRD-442 #1) #1628
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
Scra3
wants to merge
11
commits into
feat/prd-214-server-step-mapper
Choose a base branch
from
fix/prd-442-activity-log-target
base: feat/prd-214-server-step-mapper
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e8f778b
fix(workflow-executor): operation activity log targets the acted reco…
58e5ec8
refactor(workflow): extract OperationStepExecutor and migrate MCP to …
77bc7f0
refactor(workflow): address review findings on activity-log operation
b3ee99d
refactor(workflow): extract activity-log audit into AgentWithLog
2bf18bc
chore(workflow): address AgentWithLog review nits
0399c11
fix(workflow): fail loud when a collection schema is not cached
4e12bb8
refactor(workflow): fold schema cache-or-fetch into getOrLoad
453f88a
refactor(workflow): replace SchemaCache.getOrLoad with per-run Schema…
73dd015
refactor(workflow): drop errorMessage from ActivityLogPort.markFailed
3629570
test(workflow): cover beforeCall-throws in AgentWithLog audit
8bb1ba6
refactor(workflow): drop dead schemaCache field from ExecutionContext
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
130 changes: 130 additions & 0 deletions
130
packages/workflow-executor/src/executors/agent-with-log.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,130 @@ | ||
| import type { ActivityLogPort, CreateActivityLogArgs } from '../ports/activity-log-port'; | ||
| import type { | ||
| AgentPort, | ||
| ExecuteActionQuery, | ||
| GetRecordQuery, | ||
| GetRelatedDataQuery, | ||
| GetSingleRelatedDataQuery, | ||
| UpdateRecordQuery, | ||
| } from '../ports/agent-port'; | ||
| import type SchemaResolver from '../schema-resolver'; | ||
| import type { StepUser } from '../types/execution-context'; | ||
| import type { RecordData } from '../types/validated/collection'; | ||
|
|
||
| // The audit-log target minus renderingId, which audit() stamps centrally. | ||
| export type AuditTarget = Omit<CreateActivityLogArgs, 'renderingId'>; | ||
|
|
||
| type WriteOptions = { beforeCall: () => Promise<void> }; | ||
|
|
||
| export interface AgentWithLogDeps { | ||
| agentPort: AgentPort; | ||
| activityLogPort: ActivityLogPort; | ||
| schemaResolver: SchemaResolver; | ||
| user: StepUser; | ||
| } | ||
|
|
||
| // Wraps AgentPort and emits an activity-log entry around each data-access call | ||
| // (pending → success/failed). The audit target is derived from the call: the numeric | ||
| // collectionId is resolved from the call's collection name, the recordId from its id. | ||
| // Idempotency stays in the executors: write methods run a `beforeCall` thunk between | ||
| // createPending and the side effect (the executor persists its write-ahead marker there), | ||
| // so AgentWithLog never reaches into run state. | ||
| export default class AgentWithLog { | ||
| private readonly agentPort: AgentPort; | ||
| private readonly activityLogPort: ActivityLogPort; | ||
| private readonly schemaResolver: SchemaResolver; | ||
| private readonly user: StepUser; | ||
|
|
||
| constructor(deps: AgentWithLogDeps) { | ||
| this.agentPort = deps.agentPort; | ||
| this.activityLogPort = deps.activityLogPort; | ||
| this.schemaResolver = deps.schemaResolver; | ||
| this.user = deps.user; | ||
| } | ||
|
|
||
| async getRecord(query: GetRecordQuery): Promise<RecordData> { | ||
| const collectionId = await this.resolveCollectionId(query.collection); | ||
|
|
||
| return this.audit({ action: 'index', type: 'read', collectionId, recordId: query.id }, () => | ||
| this.agentPort.getRecord(query, this.user), | ||
| ); | ||
| } | ||
|
|
||
| async getRelatedData(query: GetRelatedDataQuery): Promise<RecordData[]> { | ||
| const collectionId = await this.resolveCollectionId(query.collection); | ||
|
|
||
| return this.audit( | ||
| { action: 'listRelatedData', type: 'read', collectionId, recordId: query.id }, | ||
| () => this.agentPort.getRelatedData(query, this.user), | ||
| ); | ||
| } | ||
|
|
||
| async getSingleRelatedData(query: GetSingleRelatedDataQuery): Promise<RecordData | null> { | ||
| const collectionId = await this.resolveCollectionId(query.collection); | ||
|
|
||
| return this.audit( | ||
| { action: 'listRelatedData', type: 'read', collectionId, recordId: query.id }, | ||
| () => this.agentPort.getSingleRelatedData(query, this.user), | ||
| ); | ||
| } | ||
|
|
||
| async updateRecord(query: UpdateRecordQuery, opts: WriteOptions): Promise<RecordData> { | ||
| const collectionId = await this.resolveCollectionId(query.collection); | ||
|
|
||
| return this.audit( | ||
| { action: 'update', type: 'write', collectionId, recordId: query.id }, | ||
| () => this.agentPort.updateRecord(query, this.user), | ||
| opts.beforeCall, | ||
| ); | ||
| } | ||
|
|
||
| async executeAction(query: ExecuteActionQuery, opts: WriteOptions): Promise<unknown> { | ||
| const collectionId = await this.resolveCollectionId(query.collection); | ||
|
|
||
| return this.audit( | ||
| { action: 'action', type: 'write', collectionId, recordId: query.id }, | ||
| () => this.agentPort.executeAction(query, this.user), | ||
| opts.beforeCall, | ||
| ); | ||
| } | ||
|
|
||
| // For operations that are not AgentPort calls (e.g. MCP tool invocation): the caller | ||
| // supplies the full audit target since there is no collection name to resolve. | ||
| logged<T>( | ||
| target: AuditTarget, | ||
| run: () => Promise<T>, | ||
| opts?: { beforeCall?: () => Promise<void> }, | ||
| ): Promise<T> { | ||
| return this.audit(target, run, opts?.beforeCall); | ||
| } | ||
|
|
||
| private async audit<T>( | ||
| args: AuditTarget, | ||
| run: () => Promise<T>, | ||
| beforeCall?: () => Promise<void>, | ||
| ): Promise<T> { | ||
| const handle = await this.activityLogPort.createPending({ | ||
| renderingId: this.user.renderingId, | ||
| ...args, | ||
| }); | ||
|
|
||
| try { | ||
| if (beforeCall) await beforeCall(); | ||
| const result = await run(); | ||
| void this.activityLogPort.markSucceeded(handle); | ||
|
|
||
| return result; | ||
| } catch (err) { | ||
| // The step error is logged/surfaced by base-step-executor when rethrown, so the audit | ||
| // transition only needs the handle. | ||
| void this.activityLogPort.markFailed(handle); | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| private async resolveCollectionId(collectionName: string): Promise<string> { | ||
| const schema = await this.schemaResolver.resolve(collectionName); | ||
|
|
||
| return schema.collectionId; | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should never occur because schema is already loaded at this point