9
env?: Record<string, string>;
10
with: { script: string };
11
};
12
+type Concurrency = { group: string; 'cancel-in-progress': boolean; queue?: string };
13
type Job = {
14
+ concurrency?: Concurrency;
15
permissions: Record<string, string>;
16
steps: Step[];
17
needs?: string;
19
};
20
type Workflow = {
21
permissions: Record<string, string>;
22
+ concurrency?: Concurrency;
23
jobs: Record<'authorize' | 'classify' | 'apply', Job>;
24
on: { issues: { types: string[] }; workflow_dispatch: unknown };
25
};
34
title?: string;
35
body?: string;
36
issueNumber?: string;
34
- issue?: { pull_request?: object };
37
+ issue?: { pull_request?: object; labels?: { name: string }[] };
38
+ payload?: { action?: string; label?: { name: string } };
39
+ eventName?: 'issues' | 'workflow_dispatch';
40
+ anthropicApiKey?: string;
41
result?: unknown;
42
status?: number;
43
};
49
const classification = {
50
category: 'ts',
51
is_bug: true,
52
+ requires_support: false,
53
reason: 'SDK regression',
54
should_comment: false,
55
};
59
body = 'Reproduction steps',
60
issueNumber = '42',
61
issue = {},
62
+ payload = {},
63
+ eventName = 'issues',
64
+ anthropicApiKey = 'anthropic-secret-sentinel',
65
result = {
66
stop_reason: 'end_turn',
67
content: [{ type: 'text', text: JSON.stringify(classification) }],
75
process: {
76
env: {
77
ISSUE_NUMBER: issueNumber,
68
- ANTHROPIC_API_KEY: 'anthropic-secret-sentinel',
78
+ ANTHROPIC_API_KEY: anthropicApiKey,
79
GITHUB_TOKEN: 'github-secret-sentinel',
80
PLAIN_API_KEY: 'plain-secret-sentinel',
81
},
82
},
73
- context: { repo: { owner: 'ComposioHQ', repo: 'composio' } },
83
+ context: { repo: { owner: 'ComposioHQ', repo: 'composio' }, payload, eventName },
84
github: {
85
rest: {
86
issues: {
95
setOutput: (name: string, value: string) => {
96
outputs[name] = value;
97
},
98
+ info: () => {},
99
},
100
AbortSignal,
101
fetch: async (url: string, options: RequestOptions) => {
189
content: [{ type: 'text', text: JSON.stringify({ ...classification, category: 'shell' }) }],
190
},
191
],
192
+ [
193
+ 'string support-routing flag',
194
+ {
195
+ stop_reason: 'end_turn',
196
+ content: [
197
+ { type: 'text', text: JSON.stringify({ ...classification, requires_support: 'false' }) },
198
+ ],
199
+ },
200
+ ],
201
+ [
202
+ 'missing support-routing flag',
203
+ {
204
+ stop_reason: 'end_turn',
205
+ content: [
206
+ { type: 'text', text: JSON.stringify({ ...classification, requires_support: undefined }) },
207
+ ],
208
+ },
209
+ ],
210
[
211
'extra field',
212
{
307
test(`permission lookup HTTP ${status} cannot authorize classification or write-back`, async () => {
308
const outputs: Record<string, string> = {};
309
const execution = runInNewContext(`(async () => {\n${authorizationStep.with.script}\n})()`, {
281
- context: { actor: 'outsider', repo: { owner: 'ComposioHQ', repo: 'composio' } },
310
+ context: { actor: 'outsider', repo: { owner: 'ComposioHQ', repo: 'composio' }, payload: {} },
311
github: {
312
rest: {
313
repos: {
335
}
336
337
test('authorization controls every issue event and manual dispatch before exposing classifier secrets', () => {
309
- expect(workflow.on.issues.types).toEqual(['opened', 'reopened', 'edited']);
338
+ expect(workflow.on.issues.types).toEqual(['opened', 'reopened', 'edited', 'labeled']);
339
expect(workflow.on.workflow_dispatch).toBeDefined();
340
expect(workflow.jobs.authorize.permissions).toEqual({});
341
expect(authorizationStep.env).toBeUndefined();
342
expect(workflow.jobs.classify.needs).toBe('authorize');
343
expect(workflow.jobs.classify.if).toBe("needs.authorize.outputs.allowed == 'true'");
315
- expect(workflow.jobs.apply.if).toBeUndefined();
344
+ expect(workflow.jobs.apply.if).toBe("needs.classify.outputs.issue_number != ''");
345
+ expect(workflow.concurrency).toBeUndefined();
346
+ expect(workflow.jobs.authorize.concurrency).toBeUndefined();
347
+ expect(workflow.jobs.classify.concurrency).toBeUndefined();
348
+ expect(workflow.jobs.apply.concurrency).toEqual({
349
+ group: 'issue-triage-${{ needs.classify.outputs.issue_number }}',
350
+ 'cancel-in-progress': false,
351
+ queue: 'max',
352
+ });
353
});
354
355
+for (const issueNumber of ['42', ' 42', '042', '0x2a', '4.2e1']) {
356
+ test(`dispatch input ${JSON.stringify(issueNumber)} targets the canonical issue queue`, async () => {
357
+ const { execution, outputs } = await runClassifier({
358
+ issueNumber,
359
+ eventName: 'workflow_dispatch',
360
+ });
361
+ await execution;
362
+ expect(outputs.issue_number).toBe('42');
363
+ expect(
364
+ workflow.jobs.apply.concurrency?.group.replace(
365
+ '${{ needs.classify.outputs.issue_number }}',
366
+ outputs.issue_number
367
+ )
368
+ ).toBe('issue-triage-42');
369
+ });
370
+}
371
+
372
const applyStep = workflow.jobs.apply.steps[0];
373
type ApplyOptions = {
374
result?: unknown;
375
structuredOutput?: string;
376
state?: 'open' | 'closed';
377
plainApiKey?: string;
378
+ plainHttpStatus?: number;
379
+ plainJsonError?: string;
380
+ calls?: [string, Record<string, unknown>][];
381
+ labels?: string[];
382
+ comments?: { user: { login: string }; body: string }[];
383
+ plainState?: { thread: { id: string; ref: string } | null };
384
+ plainResponse?: (query: string, data: Record<string, unknown>) => Record<string, unknown>;
385
};
386
387
async function runApply({
389
structuredOutput = JSON.stringify(result),
390
state = 'open',
391
plainApiKey = 'plain-secret-sentinel',
392
+ plainHttpStatus = 200,
393
+ plainJsonError,
394
+ calls = [],
395
+ labels = [],
396
+ comments = [],
397
+ plainState = { thread: null },
398
+ plainResponse = (_query, data) => ({ data }),
399
}: ApplyOptions = {}) {
332
- const calls: [string, Record<string, unknown>][] = [];
400
const plainRequests: { url: string; options: RequestOptions }[] = [];
401
const failures: string[] = [];
402
const record = (name: string) => async (args: Record<string, unknown>) => {
417
payload: { issue: { number: 999 } },
418
},
419
github: {
420
+ paginate: async () => comments,
421
rest: {
422
issues: {
423
get: async (args: Record<string, unknown>) => {
430
body: 'Reported body',
431
html_url: 'https://github.com/ComposioHQ/composio/issues/42',
432
user: { login: 'reporter', id: 7 },
433
+ labels: labels.map(name => ({ name })),
434
},
435
};
436
},
441
addLabels: record('addLabels'),
442
addAssignees: record('addAssignees'),
443
createComment: record('createComment'),
444
+ listComments: record('listComments'),
445
update: record('update'),
446
},
447
users: { getByUsername: async () => ({ data: { email: null, name: 'Reporter' } }) },
454
info: () => {},
455
warning: () => {},
456
},
457
+ AbortSignal,
458
+ setTimeout: (callback: () => void) => callback(),
459
fetch: async (url: string, options: RequestOptions) => {
460
plainRequests.push({ url, options });
389
- const data = options.body.includes('upsertCustomer')
390
- ? { upsertCustomer: { customer: { id: 'c_1' } } }
391
- : { createThread: { thread: { id: 't_1', ref: 'T-1' } } };
392
- return { ok: true, json: async () => ({ data }) };
461
+ const query = JSON.parse(options.body).query as string;
462
+ let data: Record<string, unknown>;
463
+ if (query.includes('threadsByExternalId')) {
464
+ data = {
465
+ threadsByExternalId: { edges: plainState.thread ? [{ node: plainState.thread }] : [] },
466
+ };
467
+ } else if (query.includes('upsertCustomer')) {
468
+ data = { upsertCustomer: { customer: { id: 'c_1' } } };
469
+ } else {
470
+ plainState.thread = { id: 't_1', ref: 'T-1' };
471
+ data = { createThread: { thread: plainState.thread } };
472
+ }
473
+ const response = plainResponse(query, data);
474
+ return {
475
+ ok: plainHttpStatus === 200,
476
+ status: plainHttpStatus,
477
+ json: async () => {
478
+ if (plainJsonError) throw new Error(plainJsonError);
479
+ return response;
480
+ },
481
+ };
482
},
483
});
484
const named = (name: string) => calls.filter(call => call[0] === name).map(call => call[1]);
565
const support = {
566
...classification,
567
category: 'support',
568
+ requires_support: true,
569
should_comment: true,
570
...modelText,
571
};
572
483
-test('a support issue drops SDK routing, is forwarded to Plain, and gets the fixed note', async () => {
573
+test('a support bug retains its type and routing, is forwarded to Plain, and gets the fixed note', async () => {
574
const { plainRequests, named } = await runApply({ result: support });
485
- expect(named('removeLabel').map(args => args.name)).toEqual(['bug', 'ts']);
486
- expect(named('removeAssignees')).toEqual([{ ...target, assignees: ['jkomyno'] }]);
487
- expect(named('addLabels')).toEqual([{ ...target, labels: ['support'] }]);
575
+ expect(named('removeLabel')).toEqual([]);
576
+ expect(named('removeAssignees')).toEqual([]);
577
+ expect(named('addLabels')).toEqual([{ ...target, labels: ['bug', 'support'] }]);
578
expect(named('addAssignees')).toEqual([]);
579
expect(plainRequests.map(request => request.url)).toEqual([
580
'https://core-api.uk.plain.com/graphql/v1',
581
'https://core-api.uk.plain.com/graphql/v1',
582
+ 'https://core-api.uk.plain.com/graphql/v1',
583
]);
584
for (const { options } of plainRequests) {
585
expect(options.headers.Authorization).toBe('Bearer plain-secret-sentinel');
586
}
496
- const thread = JSON.parse(plainRequests[1].options.body).variables.input;
587
+ const thread = JSON.parse(plainRequests[2].options.body).variables.input;
588
expect(thread.title).toBe('[GitHub #42] Reported title');
589
expect(thread.externalId).toBe('github:ComposioHQ/composio:issues/42');
590
const comments = named('createComment');
591
expect(comments).toHaveLength(1);
501
- expect(comments[0].body).toContain('forwarded it to Plain');
592
+ expect(comments[0].body).toContain('forwarded to Plain');
593
expect(comments[0].body).not.toContain('Model-written');
594
expect(JSON.stringify(comments)).not.toContain('plain-secret-sentinel');
595
expect(named('update')).toEqual([]);
596
});
597
507
-test('a support issue without a Plain key is labeled and noted without any Plain request', async () => {
508
- const { plainRequests, named } = await runApply({ result: support, plainApiKey: '' });
509
- expect(plainRequests).toEqual([]);
510
- expect(named('addLabels')).toEqual([{ ...target, labels: ['support'] }]);
511
- const comments = named('createComment');
512
- expect(comments).toHaveLength(1);
513
- expect(comments[0].body).toContain('for support team follow-up');
598
+test('a missing Plain key fails visibly instead of claiming forwarding succeeded', async () => {
599
+ await expect(runApply({ result: support, plainApiKey: '' })).rejects.toThrow('PLAIN_API_KEY');
600
});
601
602
for (const [label, structuredOutput] of [
611
expect(plainRequests).toEqual([]);
612
});
613
}
614
+
615
+for (const [label, permission, expected] of [
616
+ ['triage', 'write', 'true'],
617
+ ['support', 'maintain', 'true'],
618
+ ['bug', 'admin', 'false'],
619
+ ['support', 'read', 'false'],
620
+ ['triage', 'triage', 'false'],
621
+] as const) {
622
+ test(`a ${label} label applied with ${permission} access authorizes triage as ${expected}`, async () => {
623
+ const outputs: Record<string, string> = {};
624
+ await runInNewContext(`(async () => {\n${authorizationStep.with.script}\n})()`, {
625
+ context: {
626
+ actor: 'labeler',
627
+ repo: { owner: 'ComposioHQ', repo: 'composio' },
628
+ payload: { action: 'labeled', label: { name: label } },
629
+ },
630
+ github: {
631
+ rest: {
632
+ repos: { getCollaboratorPermissionLevel: async () => ({ data: { permission } }) },
633
+ },
634
+ },
635
+ core: {
636
+ setOutput: (key: string, value: string) => {
637
+ outputs[key] = value;
638
+ },
639
+ info: () => {},
640
+ },
641
+ });
642
+ expect(outputs.allowed).toBe(expected);
643
+ });
644
+}
645
+
646
+test('an explicitly support-labeled issue forwards without a model call or Anthropic key', async () => {
647
+ const { execution, outputs, requests } = await runClassifier({
648
+ issue: { labels: [{ name: 'support' }, { name: 'bug' }, { name: 'cli' }] },
649
+ payload: { action: 'labeled', label: { name: 'support' } },
650
+ anthropicApiKey: '',
651
+ });
652
+ await execution;
653
+ expect(requests).toEqual([]);
654
+ expect(outputs.issue_number).toBe('42');
655
+ expect(JSON.parse(outputs.structured_output)).toMatchObject({
656
+ category: 'other',
657
+ is_bug: true,
658
+ requires_support: true,
659
+ });
660
+});
661
+
662
+for (const [label, eventName, payload] of [
663
+ ['triage label', 'issues', { action: 'labeled', label: { name: 'triage' } }],
664
+ ['manual dispatch', 'workflow_dispatch', {}],
665
+] as const) {
666
+ test(`an explicit ${label} reclassifies a support issue and retains its routing`, async () => {
667
+ const { execution, outputs, requests } = await runClassifier({
668
+ issue: { labels: [{ name: 'support' }, { name: 'triage' }] },
669
+ eventName,
670
+ payload,
671
+ });
672
+ await execution;
673
+ expect(requests).toHaveLength(1);
674
+ const { named } = await runApply({
675
+ structuredOutput: outputs.structured_output,
676
+ labels: ['support', 'triage'],
677
+ });
678
+ expect(named('addLabels')).toEqual([{ ...target, labels: ['bug', 'ts', 'support'] }]);
679
+ expect(named('addAssignees')).toEqual([{ ...target, assignees: ['jkomyno'] }]);
680
+ expect(named('removeLabel')).toEqual([{ ...target, name: 'triage' }]);
681
+ });
682
+}
683
+
684
+test('a removed support label does not trigger stale forwarding', async () => {
685
+ const { execution, outputs, requests } = await runClassifier({
686
+ payload: { action: 'labeled', label: { name: 'support' } },
687
+ });
688
+ await execution;
689
+ expect(outputs).toEqual({});
690
+ expect(requests).toEqual([]);
691
+});
692
+
693
+test('a backend feature receives both feature-request and support and stays open', async () => {
694
+ const { named, plainRequests } = await runApply({
695
+ result: {
696
+ ...classification,
697
+ category: 'feature-request',
698
+ is_bug: false,
699
+ requires_support: true,
700
+ },
701
+ });
702
+ expect(named('addLabels')).toEqual([{ ...target, labels: ['feature-request', 'support'] }]);
703
+ expect(named('update')).toEqual([]);
704
+ expect(plainRequests).toHaveLength(3);
705
+});
706
+
707
+test('mixed SDK and backend work retains SDK ownership when routed to support', async () => {
708
+ const { named } = await runApply({
709
+ result: { ...classification, requires_support: true },
710
+ labels: ['bug', 'ts'],
711
+ });
712
+ expect(named('addLabels')).toEqual([{ ...target, labels: ['bug', 'ts', 'support'] }]);
713
+ expect(named('addAssignees')).toEqual([{ ...target, assignees: ['jkomyno'] }]);
714
+ expect(named('removeLabel')).toEqual([]);
715
+ expect(named('removeAssignees')).toEqual([]);
716
+});
717
+
718
+test('a tool request requiring coordination is forwarded without automatic closure', async () => {
719
+ const { named } = await runApply({ result: { ...toolRequest, requires_support: true } });
720
+ expect(named('addLabels')).toEqual([{ ...target, labels: ['tool-request', 'support'] }]);
721
+ expect(named('update')).toEqual([]);
722
+ expect(named('createComment')).toHaveLength(1);
723
+ expect(named('createComment')[0].body).toContain('forwarded to Plain');
724
+});
725
+
726
+test('an existing support label is retained even if classification suggests repository-only work', async () => {
727
+ const { named, plainRequests } = await runApply({ labels: ['support'] });
728
+ expect(named('addLabels')[0].labels).toContain('support');
729
+ expect(plainRequests).toHaveLength(3);
730
+});
731
+
732
+test('an ambiguous create timeout is recovered through lookup, without a second thread', async () => {
733
+ const queries: string[] = [];
734
+ const { plainRequests, named } = await runApply({
735
+ result: support,
736
+ plainResponse: (query, data) => {
737
+ queries.push(query);
738
+ if (query.includes('mutation createThread')) throw new Error('Response lost after commit');
739
+ return { data };
740
+ },
741
+ });
742
+ expect(queries.filter(query => query.includes('mutation createThread'))).toHaveLength(1);
743
+ expect(queries.filter(query => query.includes('threadsByExternalId'))).toHaveLength(2);
744
+ expect(plainRequests).toHaveLength(4);
745
+ expect(named('createComment')).toHaveLength(1);
746
+});
747
+
748
+test('reruns reuse the Plain thread and do not duplicate a bot forwarding note', async () => {
749
+ const plainState = { thread: null as { id: string; ref: string } | null };
750
+ const first = await runApply({ result: support, plainState });
751
+ const second = await runApply({
752
+ result: support,
753
+ plainState,
754
+ comments: [
755
+ {
756
+ user: { login: 'github-actions[bot]' },
757
+ body: String(first.named('createComment')[0].body),
758
+ },
759
+ ],
760
+ });
761
+ expect(second.plainRequests).toHaveLength(1);
762
+ expect(second.named('createComment')).toEqual([]);
763
+});
764
+
765
+test('a reporter cannot suppress the bot note by posting its marker', async () => {
766
+ const { named } = await runApply({
767
+ result: support,
768
+ comments: [{ user: { login: 'reporter' }, body: '<!-- composio-support-forwarded -->' }],
769
+ });
770
+ expect(named('createComment')).toHaveLength(1);
771
+});
772
+
773
+test('Plain GraphQL failures are retried three times, then fail visibly', async () => {
774
+ let requests = 0;
775
+ await expect(
776
+ runApply({
777
+ result: support,
778
+ plainResponse: () => {
779
+ requests++;
780
+ return { errors: [{ message: 'Unavailable' }] };
781
+ },
782
+ })
783
+ ).rejects.toThrow('Plain API request failed');
784
+ expect(requests).toBe(3);
785
+});
786
+
787
+for (const code of ['FORBIDDEN', 'RATE_LIMITED', 'GRAPHQL_VALIDATION_FAILED']) {
788
+ test(`Plain HTTP-200 GraphQL ${code} errors report a safe operation and code`, async () => {
789
+ let failure;
790
+ try {
791
+ await runApply({
792
+ result: support,
793
+ plainResponse: () => ({
794
+ errors: [
795
+ {
796
+ message: 'plain-secret-sentinel Reported body reporter@example.com',
797
+ extensions: { code },
798
+ },
799
+ ],
800
+ }),
801
+ });
802
+ } catch (error) {
803
+ failure = error;
804
+ }
805
+ expect(failure).toBeDefined();
806
+ const message = String(failure);
807
+ expect(message).toContain('supportThread');
808
+ expect(message).toContain(code);
809
+ expect(message).not.toContain('plain-secret-sentinel');
810
+ expect(message).not.toContain('Reported body');
811
+ expect(message).not.toContain('reporter@example.com');
812
+ });
813
+}
814
+
815
+test('Plain diagnostics omit unknown error codes and raw response values', async () => {
816
+ await expect(
817
+ runApply({
818
+ result: support,
819
+ plainResponse: () => ({
820
+ errors: [
821
+ {
822
+ message: 'plain-secret-sentinel',
823
+ extensions: { code: 'plain-secret-sentinel' },
824
+ },
825
+ ],
826
+ }),
827
+ })
828
+ ).rejects.toThrow('unrecognized GraphQL error');
829
+});
830
+
831
+test('Plain non-JSON failures retain HTTP context without exposing parser response text', async () => {
832
+ let failure;
833
+ try {
834
+ await runApply({
835
+ result: support,
836
+ plainHttpStatus: 503,
837
+ plainJsonError: 'plain-secret-sentinel',
838
+ });
839
+ } catch (error) {
840
+ failure = error;
841
+ }
842
+ expect(failure).toBeDefined();
843
+ expect(String(failure)).toContain('HTTP 503; invalid JSON response');
844
+ expect(String(failure)).not.toContain('plain-secret-sentinel');
845
+});
846
+
847
+test('a Plain HTTP failure leaves support and triage labels in place without a success note', async () => {
848
+ const calls: [string, Record<string, unknown>][] = [];
849
+ await expect(
850
+ runApply({
851
+ result: support,
852
+ labels: ['triage'],
853
+ plainHttpStatus: 503,
854
+ calls,
855
+ })
856
+ ).rejects.toThrow('HTTP 503');
857
+ expect(calls.filter(([name]) => name === 'addLabels')[0][1].labels).toContain('support');
858
+ expect(calls.filter(([name]) => name === 'removeLabel' || name === 'createComment')).toEqual([]);
859
+});
860
+
861
+test('Plain mutation-level failures cannot be reported as successful forwarding', async () => {
862
+ const calls: [string, Record<string, unknown>][] = [];
863
+ await expect(
864
+ runApply({
865
+ result: support,
866
+ calls,
867
+ plainResponse: (query, data) =>
868
+ query.includes('upsertCustomer')
869
+ ? { data: { upsertCustomer: { error: { type: 'FORBIDDEN' } } } }
870
+ : { data },
871
+ })
872
+ ).rejects.toThrow('Could not upsert Plain support customer');
873
+ expect(calls.filter(([name]) => name === 'createComment')).toEqual([]);
874
+});
875
+
876
+test('a triage request is cleared only after successful forwarding', async () => {
877
+ const { named } = await runApply({ result: support, labels: ['triage'] });
878
+ expect(named('removeLabel')).toEqual([{ ...target, name: 'triage' }]);
879
+});