Skip to content

Commit 2c2c72e

Browse files
Expose sanitized command principal context for durable operator actions.
Expose command principal context
1 parent a94a173 commit 2c2c72e

7 files changed

Lines changed: 81 additions & 1 deletion

File tree

src/V2/CommandContext.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ public function withIntake(string $mode, string $groupId): self
128128
]);
129129
}
130130

131+
public function withPrincipal(string $type, string $id, ?string $label = null): self
132+
{
133+
return $this->with([
134+
'principal' => array_filter([
135+
'type' => $type,
136+
'id' => $id,
137+
'label' => $label,
138+
], static fn (mixed $value): bool => is_string($value) && trim($value) !== ''),
139+
]);
140+
}
141+
131142
/**
132143
* @return array<string, mixed>
133144
*/

src/V2/Models/WorkflowCommand.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public function publicContext(): array
210210
{
211211
$workflow = $this->commandContext()['workflow'] ?? null;
212212
$intake = $this->commandContext()['intake'] ?? null;
213+
$principal = $this->commandContext()['principal'] ?? null;
213214

214215
$publicWorkflow = is_array($workflow)
215216
? array_filter([
@@ -239,12 +240,54 @@ public function publicContext(): array
239240
], static fn (mixed $value): bool => $value !== null && $value !== '')
240241
: [];
241242

243+
$publicPrincipal = is_array($principal)
244+
? array_filter([
245+
'type' => is_string($principal['type'] ?? null)
246+
? $principal['type']
247+
: null,
248+
'id' => is_string($principal['id'] ?? null)
249+
? $principal['id']
250+
: null,
251+
'label' => is_string($principal['label'] ?? null)
252+
? $principal['label']
253+
: null,
254+
], static fn (mixed $value): bool => $value !== null && $value !== '')
255+
: [];
256+
242257
return array_filter([
243258
'workflow' => $publicWorkflow === [] ? null : $publicWorkflow,
244259
'intake' => $publicIntake === [] ? null : $publicIntake,
260+
'principal' => $publicPrincipal === [] ? null : $publicPrincipal,
245261
], static fn (mixed $value): bool => $value !== null);
246262
}
247263

264+
public function principalType(): ?string
265+
{
266+
$principal = $this->commandContext()['principal'] ?? null;
267+
268+
return is_array($principal) && is_string($principal['type'] ?? null)
269+
? $principal['type']
270+
: null;
271+
}
272+
273+
public function principalId(): ?string
274+
{
275+
$principal = $this->commandContext()['principal'] ?? null;
276+
277+
return is_array($principal) && is_string($principal['id'] ?? null)
278+
? $principal['id']
279+
: null;
280+
}
281+
282+
public function principalLabel(): ?string
283+
{
284+
$principal = $this->commandContext()['principal'] ?? null;
285+
286+
return is_array($principal) && is_string($principal['label'] ?? null)
287+
? $principal['label']
288+
: null;
289+
}
290+
248291
public function callerLabel(): ?string
249292
{
250293
$caller = $this->commandContext()['caller'] ?? null;

src/V2/Models/WorkflowHistoryEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ private static function commandSnapshot(WorkflowCommand $command): array
148148
'source' => $command->source,
149149
'context' => $publicContext === [] ? null : $publicContext,
150150
'caller_label' => $command->callerLabel(),
151+
'principal_type' => $command->principalType(),
152+
'principal_id' => $command->principalId(),
153+
'principal_label' => $command->principalLabel(),
151154
'auth_status' => $command->authStatus(),
152155
'auth_method' => $command->authMethod(),
153156
'request_method' => $command->requestMethod(),

src/V2/Support/HistoryExport.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,9 @@ private static function command(WorkflowCommand $command): array
10201020
'source' => $command->source,
10211021
'context' => $command->publicContext(),
10221022
'caller_label' => $command->callerLabel(),
1023+
'principal_type' => $command->principalType(),
1024+
'principal_id' => $command->principalId(),
1025+
'principal_label' => $command->principalLabel(),
10231026
'auth_status' => $command->authStatus(),
10241027
'auth_method' => $command->authMethod(),
10251028
'request_method' => $command->requestMethod(),

src/V2/Support/HistoryTimeline.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,9 @@ private static function commandMetadata(
575575
$snapshot['context'] ?? null
576576
) ?? ($publicContext === [] ? null : $publicContext),
577577
'caller_label' => self::stringValue($snapshot['caller_label'] ?? null) ?? $command?->callerLabel(),
578+
'principal_type' => self::stringValue($snapshot['principal_type'] ?? null) ?? $command?->principalType(),
579+
'principal_id' => self::stringValue($snapshot['principal_id'] ?? null) ?? $command?->principalId(),
580+
'principal_label' => self::stringValue($snapshot['principal_label'] ?? null) ?? $command?->principalLabel(),
578581
'auth_status' => self::stringValue($snapshot['auth_status'] ?? null) ?? $command?->authStatus(),
579582
'auth_method' => self::stringValue($snapshot['auth_method'] ?? null) ?? $command?->authMethod(),
580583
'request_method' => self::stringValue($snapshot['request_method'] ?? null) ?? $command?->requestMethod(),

src/V2/Support/RunDetailView.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ public static function forRun(WorkflowRun $run, ?int $timelineLimit = null): arr
299299
'source' => $command->source,
300300
'context' => $command->publicContext(),
301301
'caller_label' => $command->callerLabel(),
302+
'principal_type' => $command->principalType(),
303+
'principal_id' => $command->principalId(),
304+
'principal_label' => $command->principalLabel(),
302305
'auth_status' => $command->authStatus(),
303306
'auth_method' => $command->authMethod(),
304307
'request_method' => $command->requestMethod(),

tests/Feature/V2/V2RunDetailViewTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,11 @@ public function testRunDetailViewOmitsRawRequestContextForWebhookCommands(): voi
22292229
'status' => 'authorized',
22302230
'method' => 'token',
22312231
],
2232+
'principal' => [
2233+
'type' => 'user',
2234+
'id' => '42',
2235+
'label' => 'Taylor Otwell',
2236+
],
22322237
'request' => [
22332238
'method' => 'POST',
22342239
'path' => '/webhooks/instances/detail-command-context/signals/name-provided',
@@ -2260,8 +2265,17 @@ public function testRunDetailViewOmitsRawRequestContextForWebhookCommands(): voi
22602265
$detail = RunDetailView::forRun($run->fresh(['summary']));
22612266

22622267
$this->assertCount(1, $detail['commands']);
2263-
$this->assertSame([], $detail['commands'][0]['context']);
2268+
$this->assertSame([
2269+
'principal' => [
2270+
'type' => 'user',
2271+
'id' => '42',
2272+
'label' => 'Taylor Otwell',
2273+
],
2274+
], $detail['commands'][0]['context']);
22642275
$this->assertSame('Webhook', $detail['commands'][0]['caller_label']);
2276+
$this->assertSame('user', $detail['commands'][0]['principal_type']);
2277+
$this->assertSame('42', $detail['commands'][0]['principal_id']);
2278+
$this->assertSame('Taylor Otwell', $detail['commands'][0]['principal_label']);
22652279
$this->assertSame('authorized', $detail['commands'][0]['auth_status']);
22662280
$this->assertSame('token', $detail['commands'][0]['auth_method']);
22672281
$this->assertSame('POST', $detail['commands'][0]['request_method']);

0 commit comments

Comments
 (0)