@tetsuo-ai/marketplace-react 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/README.md +93 -47
  3. package/dist/chunk-7NKRSPPN.js +227 -0
  4. package/dist/chunk-7NKRSPPN.js.map +1 -0
  5. package/dist/{chunk-BNILOX3O.js → chunk-CDDHUFMG.js} +38 -6
  6. package/dist/chunk-CDDHUFMG.js.map +1 -0
  7. package/dist/chunk-FIDFPM5A.cjs +40 -0
  8. package/dist/chunk-FIDFPM5A.cjs.map +1 -0
  9. package/dist/chunk-IJKWKY4H.cjs +1370 -0
  10. package/dist/chunk-IJKWKY4H.cjs.map +1 -0
  11. package/dist/chunk-JTSG5FGC.cjs +397 -0
  12. package/dist/chunk-JTSG5FGC.cjs.map +1 -0
  13. package/dist/chunk-ONC6L3IX.cjs +97 -0
  14. package/dist/chunk-ONC6L3IX.cjs.map +1 -0
  15. package/dist/chunk-R76EUYDI.cjs +233 -0
  16. package/dist/chunk-R76EUYDI.cjs.map +1 -0
  17. package/dist/chunk-RG5JZ6JL.js +378 -0
  18. package/dist/chunk-RG5JZ6JL.js.map +1 -0
  19. package/dist/components/index.cjs +158 -1513
  20. package/dist/components/index.cjs.map +1 -1
  21. package/dist/components/index.d.cts +2 -2
  22. package/dist/components/index.d.ts +2 -2
  23. package/dist/components/index.js +2 -2
  24. package/dist/hooks/index.cjs +693 -457
  25. package/dist/hooks/index.cjs.map +1 -1
  26. package/dist/hooks/index.d.cts +109 -28
  27. package/dist/hooks/index.d.ts +109 -28
  28. package/dist/hooks/index.js +659 -178
  29. package/dist/hooks/index.js.map +1 -1
  30. package/dist/{index-DQ3Xovkq.d.ts → index-BAyr1ODZ.d.ts} +76 -6
  31. package/dist/{index-UxuvIlmD.d.cts → index-C20NVU7_.d.cts} +76 -6
  32. package/dist/index.cjs +284 -1752
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +7 -8
  35. package/dist/index.d.ts +7 -8
  36. package/dist/index.js +64 -4
  37. package/dist/index.js.map +1 -1
  38. package/dist/signers/index.cjs +22 -161
  39. package/dist/signers/index.cjs.map +1 -1
  40. package/dist/signers/index.d.cts +16 -11
  41. package/dist/signers/index.d.ts +16 -11
  42. package/dist/signers/index.js +1 -1
  43. package/dist/testing/index.d.cts +1 -1
  44. package/dist/testing/index.d.ts +1 -1
  45. package/dist/theme/agenc-tailwind-preset.cjs +134 -108
  46. package/dist/{types-jvISR6bc.d.cts → types-B10rndEI.d.cts} +16 -7
  47. package/dist/{types-jvISR6bc.d.ts → types-B10rndEI.d.ts} +16 -7
  48. package/dist/{useAgentTrackRecord-BCvRHYM2.d.cts → useAgentTrackRecord-Ch7gHTFj.d.cts} +40 -3
  49. package/dist/{useAgentTrackRecord-BCvRHYM2.d.ts → useAgentTrackRecord-Ch7gHTFj.d.ts} +40 -3
  50. package/package.json +6 -3
  51. package/src/components/GuaranteedBadge.tsx +73 -0
  52. package/src/components/HireCheckoutModal.tsx +5 -2
  53. package/src/components/index.ts +4 -0
  54. package/src/components/strings.ts +8 -0
  55. package/src/hooks/index.ts +14 -0
  56. package/src/hooks/internal.ts +179 -22
  57. package/src/hooks/useAgentTrackRecord.ts +5 -2
  58. package/src/hooks/useCompletionBond.ts +191 -0
  59. package/src/hooks/useDispute.ts +46 -9
  60. package/src/hooks/useHire.ts +113 -23
  61. package/src/hooks/useHumanlessHireFlow.ts +335 -115
  62. package/src/hooks/useListing.ts +2 -2
  63. package/src/hooks/useListings.ts +2 -2
  64. package/src/hooks/useRateHire.ts +32 -5
  65. package/src/hooks/useReferrerEarnings.ts +4 -1
  66. package/src/hooks/useSubmissionReview.ts +95 -32
  67. package/src/hooks/useTaskActivation.ts +51 -9
  68. package/src/hooks/useTaskGuarantee.ts +123 -0
  69. package/src/hooks/useTaskLifecycle.ts +104 -24
  70. package/src/hooks/useTaskStatus.ts +121 -26
  71. package/src/hooks/useTaskWork.ts +76 -12
  72. package/src/hooks/useWalletSigner.ts +11 -16
  73. package/src/index.ts +5 -0
  74. package/src/provider/AgencProvider.tsx +95 -3
  75. package/src/provider/referrer.ts +1 -1
  76. package/src/signers/README.md +13 -7
  77. package/src/signers/index.ts +6 -1
  78. package/src/signers/strings.ts +13 -1
  79. package/src/signers/types.ts +16 -5
  80. package/src/signers/wallet-account.ts +115 -29
  81. package/src/signers/wallet-adapter.ts +38 -4
  82. package/src/theme/agenc-tailwind-preset.cjs +134 -108
  83. package/src/types.ts +32 -3
  84. package/dist/chunk-BNILOX3O.js.map +0 -1
  85. package/dist/chunk-BTAVED7S.js +0 -226
  86. package/dist/chunk-BTAVED7S.js.map +0 -1
  87. package/dist/chunk-ON6XR4XO.js +0 -128
  88. package/dist/chunk-ON6XR4XO.js.map +0 -1
@@ -18,10 +18,16 @@
18
18
  * @module hooks/useSubmissionReview
19
19
  */
20
20
  import { useMutation } from "@tanstack/react-query";
21
- import { useCallback } from "react";
21
+ import { useCallback, useRef } from "react";
22
22
  import { facade, type facade as facadeNs } from "@tetsuo-ai/marketplace-sdk";
23
23
  import { useAgencContext } from "../provider/context.js";
24
- import { requireClient } from "./internal.js";
24
+ import type { MarketplaceClient } from "../types.js";
25
+ import {
26
+ requireClient,
27
+ snapshotFixedBytes32,
28
+ snapshotRecord,
29
+ stabilizeSelectedTransactionSigner,
30
+ } from "./internal.js";
25
31
 
26
32
  /** Per-call input for `accept` (task auto-bound; signer defaults to the client). */
27
33
  export type AcceptInput = Omit<
@@ -50,6 +56,11 @@ export type RequestChangesInput = Omit<
50
56
  /** Lifecycle status of a review verb. */
51
57
  export type ReviewStatus = "idle" | "pending" | "success" | "error";
52
58
 
59
+ interface QueuedReviewInput<TInput> {
60
+ readonly client: MarketplaceClient;
61
+ readonly input: TInput;
62
+ }
63
+
53
64
  /** A single review verb's surface. */
54
65
  export interface ReviewAction<TInput> {
55
66
  /** Run the action. Resolves to the tx signature; rejects with `AgencError`. */
@@ -95,39 +106,40 @@ export function useSubmissionReview(
95
106
  taskPda: Parameters<typeof facadeNs.acceptTaskResult>[0]["task"],
96
107
  ): UseSubmissionReviewResult {
97
108
  const ctx = useAgencContext();
109
+ const lastAction = useRef<"accept" | "reject" | "requestChanges" | null>(
110
+ null,
111
+ );
98
112
 
99
- const acceptMut = useMutation<string, Error, AcceptInput>({
100
- mutationFn: async (input) => {
101
- const client = requireClient(ctx.client);
113
+ const acceptMut = useMutation<string, Error, QueuedReviewInput<AcceptInput>>({
114
+ mutationFn: async ({ client, input }) => {
102
115
  const { signature } = await client.acceptTaskResult({
103
116
  ...input,
104
117
  task: taskPda,
105
- creator: input.creator ?? client.signer,
106
118
  } as Parameters<typeof facadeNs.acceptTaskResult>[0]);
107
119
  return signature;
108
120
  },
109
121
  });
110
122
 
111
- const rejectMut = useMutation<string, Error, RejectInput>({
112
- mutationFn: async (input) => {
113
- const client = requireClient(ctx.client);
123
+ const rejectMut = useMutation<string, Error, QueuedReviewInput<RejectInput>>({
124
+ mutationFn: async ({ client, input }) => {
114
125
  const ix = await facade.rejectTaskResult({
115
126
  ...input,
116
127
  task: taskPda,
117
- creator: input.creator ?? client.signer,
118
128
  } as Parameters<typeof facadeNs.rejectTaskResult>[0]);
119
129
  const { signature } = await client.send([ix]);
120
130
  return signature;
121
131
  },
122
132
  });
123
133
 
124
- const requestChangesMut = useMutation<string, Error, RequestChangesInput>({
125
- mutationFn: async (input) => {
126
- const client = requireClient(ctx.client);
134
+ const requestChangesMut = useMutation<
135
+ string,
136
+ Error,
137
+ QueuedReviewInput<RequestChangesInput>
138
+ >({
139
+ mutationFn: async ({ client, input }) => {
127
140
  const ix = await facade.requestChanges({
128
141
  ...input,
129
142
  task: taskPda,
130
- creator: input.creator ?? client.signer,
131
143
  } as Parameters<typeof facadeNs.requestChanges>[0]);
132
144
  const { signature } = await client.send([ix]);
133
145
  return signature;
@@ -135,33 +147,83 @@ export function useSubmissionReview(
135
147
  });
136
148
 
137
149
  const accept = useCallback<ReviewAction<AcceptInput>>(
138
- (input) => acceptMut.mutateAsync(input),
139
- [acceptMut],
150
+ async (input) => {
151
+ const client = requireClient(ctx.client);
152
+ const detachedInput = snapshotRecord(input);
153
+ const creator = stabilizeSelectedTransactionSigner(
154
+ client.signer,
155
+ detachedInput.creator,
156
+ );
157
+ const snapshottedInput = snapshotRecord({
158
+ ...detachedInput,
159
+ creator,
160
+ ...(detachedInput.bidSettlement === undefined
161
+ ? {}
162
+ : { bidSettlement: snapshotRecord(detachedInput.bidSettlement) }),
163
+ }) as AcceptInput;
164
+ lastAction.current = "accept";
165
+ return acceptMut.mutateAsync({ client, input: snapshottedInput });
166
+ },
167
+ [acceptMut, ctx.client],
140
168
  );
141
169
  const reject = useCallback<ReviewAction<RejectInput>>(
142
- (input) => rejectMut.mutateAsync(input),
143
- [rejectMut],
170
+ async (input) => {
171
+ const client = requireClient(ctx.client);
172
+ const detachedInput = snapshotRecord(input);
173
+ const creator = stabilizeSelectedTransactionSigner(
174
+ client.signer,
175
+ detachedInput.creator,
176
+ );
177
+ const snapshottedInput = snapshotRecord({
178
+ ...detachedInput,
179
+ creator,
180
+ rejectionHash: snapshotFixedBytes32(
181
+ detachedInput.rejectionHash,
182
+ "useSubmissionReview.reject: rejectionHash",
183
+ ),
184
+ }) as RejectInput;
185
+ lastAction.current = "reject";
186
+ return rejectMut.mutateAsync({ client, input: snapshottedInput });
187
+ },
188
+ [ctx.client, rejectMut],
144
189
  );
145
190
  const requestChanges = useCallback<ReviewAction<RequestChangesInput>>(
146
- (input) => requestChangesMut.mutateAsync(input),
147
- [requestChangesMut],
191
+ async (input) => {
192
+ const client = requireClient(ctx.client);
193
+ const detachedInput = snapshotRecord(input);
194
+ const creator = stabilizeSelectedTransactionSigner(
195
+ client.signer,
196
+ detachedInput.creator,
197
+ );
198
+ const snapshottedInput = snapshotRecord({
199
+ ...detachedInput,
200
+ creator,
201
+ changesHash: snapshotFixedBytes32(
202
+ detachedInput.changesHash,
203
+ "useSubmissionReview.requestChanges: changesHash",
204
+ ),
205
+ }) as RequestChangesInput;
206
+ lastAction.current = "requestChanges";
207
+ return requestChangesMut.mutateAsync({
208
+ client,
209
+ input: snapshottedInput,
210
+ });
211
+ },
212
+ [ctx.client, requestChangesMut],
148
213
  );
149
214
 
150
215
  const anyPending =
151
216
  acceptMut.isPending || rejectMut.isPending || requestChangesMut.isPending;
152
- const status: ReviewStatus = anyPending
153
- ? "pending"
154
- : statusOf(
155
- // Surface the most recently-touched verb's terminal state.
156
- [acceptMut, rejectMut, requestChangesMut].find(
157
- (m) => m.isError || m.isSuccess,
158
- ) ?? acceptMut,
159
- );
217
+ const latest =
218
+ lastAction.current === "reject"
219
+ ? rejectMut
220
+ : lastAction.current === "requestChanges"
221
+ ? requestChangesMut
222
+ : acceptMut;
223
+ const status: ReviewStatus = anyPending ? "pending" : statusOf(latest);
160
224
 
161
- const error =
162
- acceptMut.error ?? rejectMut.error ?? requestChangesMut.error ?? null;
163
- const signature =
164
- acceptMut.data ?? rejectMut.data ?? requestChangesMut.data ?? null;
225
+ const error = latest.error ?? null;
226
+ const signature = latest.data ?? null;
165
227
 
166
228
  return {
167
229
  accept,
@@ -171,6 +233,7 @@ export function useSubmissionReview(
171
233
  error,
172
234
  signature,
173
235
  reset: () => {
236
+ lastAction.current = null;
174
237
  acceptMut.reset();
175
238
  rejectMut.reset();
176
239
  requestChangesMut.reset();
@@ -5,6 +5,8 @@ import { useAgencContext } from "../provider/context.js";
5
5
  import {
6
6
  mutationStatusOf,
7
7
  requireClient,
8
+ snapshotFixedBytes32,
9
+ stabilizeSelectedTransactionSigner,
8
10
  type MutationStatus,
9
11
  } from "./internal.js";
10
12
  import { resolveActivationModerationAccounts } from "./moderation-attestor.js";
@@ -27,13 +29,30 @@ export interface UseTaskActivationResult {
27
29
  reset: () => void;
28
30
  }
29
31
 
32
+ interface TaskActivationMutationVariables {
33
+ client: ReturnType<typeof requireClient>;
34
+ input: TaskActivationInput;
35
+ taskPda: Parameters<typeof facadeNs.setTaskJobSpec>[0]["task"];
36
+ creator: Parameters<typeof facadeNs.setTaskJobSpec>[0]["creator"];
37
+ orchestrationRpcUrl: ReturnType<
38
+ typeof useAgencContext
39
+ >["orchestrationRpcUrl"];
40
+ orchestrationRpc: ReturnType<typeof useAgencContext>["orchestrationRpc"];
41
+ }
42
+
30
43
  export function useTaskActivation(
31
44
  taskPda: Parameters<typeof facadeNs.setTaskJobSpec>[0]["task"],
32
45
  ): UseTaskActivationResult {
33
46
  const ctx = useAgencContext();
34
- const mutation = useMutation<string, Error, TaskActivationInput>({
35
- mutationFn: async (input) => {
36
- const client = requireClient(ctx.client);
47
+ const mutation = useMutation<string, Error, TaskActivationMutationVariables>({
48
+ mutationFn: async ({
49
+ client,
50
+ input,
51
+ taskPda: snapshottedTaskPda,
52
+ creator,
53
+ orchestrationRpcUrl,
54
+ orchestrationRpc,
55
+ }) => {
37
56
  // P1.2: the publish gate names an explicit `moderator` (supplied by the
38
57
  // caller — the trust decision) and needs the roster-entry PDA when that
39
58
  // moderator is a registered attestor, plus a record override when the
@@ -46,24 +65,47 @@ export function useTaskActivation(
46
65
  const resolved = callerResolved
47
66
  ? {}
48
67
  : await resolveActivationModerationAccounts({
49
- rpcUrl: ctx.rpcUrl,
50
- task: taskPda,
68
+ rpcUrl: orchestrationRpcUrl,
69
+ ...(orchestrationRpc === null ? {} : { rpc: orchestrationRpc }),
70
+ task: snapshottedTaskPda,
51
71
  jobSpecHash: input.jobSpecHash,
52
72
  moderator: input.moderator,
53
73
  });
54
74
  const { signature } = await client.setTaskJobSpec({
55
75
  ...input,
56
76
  ...resolved,
57
- task: taskPda,
58
- creator: input.creator ?? client.signer,
77
+ task: snapshottedTaskPda,
78
+ creator,
59
79
  } as Parameters<typeof facadeNs.setTaskJobSpec>[0]);
60
80
  return signature;
61
81
  },
62
82
  });
63
83
 
64
84
  const activate = useCallback(
65
- (input: TaskActivationInput) => mutation.mutateAsync(input),
66
- [mutation],
85
+ async (input: TaskActivationInput) => {
86
+ const client = requireClient(ctx.client);
87
+ const detachedInput = { ...input };
88
+ const snapshottedInput = {
89
+ ...detachedInput,
90
+ jobSpecHash: snapshotFixedBytes32(
91
+ detachedInput.jobSpecHash,
92
+ "useTaskActivation: jobSpecHash",
93
+ ),
94
+ };
95
+ const creator = stabilizeSelectedTransactionSigner(
96
+ client.signer,
97
+ detachedInput.creator,
98
+ );
99
+ return mutation.mutateAsync({
100
+ client,
101
+ input: snapshottedInput,
102
+ taskPda,
103
+ creator,
104
+ orchestrationRpcUrl: ctx.orchestrationRpcUrl,
105
+ orchestrationRpc: ctx.orchestrationRpc,
106
+ });
107
+ },
108
+ [ctx, mutation],
67
109
  );
68
110
 
69
111
  return {
@@ -0,0 +1,123 @@
1
+ /**
2
+ * `useTaskGuarantee(taskPda)` — a task's completion-bond ("Guaranteed Hire")
3
+ * state, read-only.
4
+ *
5
+ * Guaranteed Hire is the productized read of the LIVE completion-bond
6
+ * machinery: a worker who posts a completion bond stakes 25% of the reward on
7
+ * passing review, and forfeits it if the result is rejected or they lose a
8
+ * dispute. `guaranteed` is `true` iff the worker bond is posted and unresolved
9
+ * (bond PDAs are closed by every settlement exit, so live == unresolved).
10
+ *
11
+ * HONEST BOUNDARY (phase 1 — do not overclaim in UI copy): a FORFEITED bond
12
+ * pays the protocol **treasury**, not the harmed party. The buyer's protection
13
+ * today is the escrow refund on a failed review PLUS the worker's 25% skin in
14
+ * the game — the buyer does not receive the bond itself. Phase 2 (batch-2
15
+ * program work) redirects forfeiture to the harmed party.
16
+ *
17
+ * ## How it reads
18
+ *
19
+ * The bond accounts are keyed by (task, posting wallet) and the worker's
20
+ * wallet is not derivable from the Task account alone, so this is a
21
+ * `getProgramAccounts` read (`CompletionBond.task` memcmp) — the SDK's
22
+ * `fetchTaskGuarantee`. Resolution order:
23
+ * 1. `options.guaranteeReader` — an injected reader seam (tests, litesvm,
24
+ * custom transports; same pattern as `useTaskStatus`/`useDispute`);
25
+ * 2. the provider's resolved `rpcUrl` — a kit RPC is built for the read. NOTE:
26
+ * the RPC must allow `getProgramAccounts` (many public providers restrict
27
+ * it); a rejection surfaces in `error`, never a crash.
28
+ * Without either, the hook stays idle.
29
+ *
30
+ * @module hooks/useTaskGuarantee
31
+ */
32
+ import { useQuery } from "@tanstack/react-query";
33
+ import { useMemo } from "react";
34
+ import { createSolanaRpc } from "@solana/kit";
35
+ import {
36
+ fetchTaskGuarantee,
37
+ type ProgramAccountsSource,
38
+ type TaskGuarantee,
39
+ } from "@tetsuo-ai/marketplace-sdk";
40
+ import { useAgencContext } from "../provider/context.js";
41
+ import type { Address } from "../types.js";
42
+ import { pdaKey, queryKeys } from "./internal.js";
43
+
44
+ /** Reads a task's {@link TaskGuarantee} (the SDK `fetchTaskGuarantee` shape). */
45
+ export type TaskGuaranteeReader = (
46
+ taskPda: Address | string,
47
+ ) => Promise<TaskGuarantee>;
48
+
49
+ /** Options for {@link useTaskGuarantee}. */
50
+ export interface UseTaskGuaranteeOptions {
51
+ /**
52
+ * Injected reader seam (wins over the provider `rpcUrl` default). Wire it
53
+ * from litesvm/gpa-sim in tests or from your own transport.
54
+ */
55
+ guaranteeReader?: TaskGuaranteeReader;
56
+ /** Disable the read. Default `true` when `taskPda` + a read source exist. */
57
+ enabled?: boolean;
58
+ }
59
+
60
+ /** Return value of {@link useTaskGuarantee}. */
61
+ export interface UseTaskGuaranteeResult {
62
+ /** The full bond state (worker/creator bonds + flag), or null until read. */
63
+ guarantee: TaskGuarantee | null;
64
+ /**
65
+ * Convenience: `guarantee?.guaranteed ?? false` — `true` iff the worker
66
+ * bond is live (posted and unresolved).
67
+ */
68
+ guaranteed: boolean;
69
+ /** True while the first read is in flight. */
70
+ isLoading: boolean;
71
+ /** The read error, or null. */
72
+ error: Error | null;
73
+ /** Force a refetch (e.g. after posting/reclaiming a bond). */
74
+ refetch: () => void;
75
+ }
76
+
77
+ /**
78
+ * Read a task's Guaranteed Hire state.
79
+ *
80
+ * @param taskPda - The Task PDA (falsy disables the read).
81
+ * @param options - Reader seam + enabled.
82
+ * @returns {@link UseTaskGuaranteeResult}.
83
+ */
84
+ export function useTaskGuarantee(
85
+ taskPda: Address | string | undefined | null,
86
+ options?: UseTaskGuaranteeOptions,
87
+ ): UseTaskGuaranteeResult {
88
+ const ctx = useAgencContext();
89
+ const injected = options?.guaranteeReader;
90
+ const rpcUrl = ctx.rpcUrl;
91
+
92
+ // Injected seam wins; else build a gPA read over the provider's rpcUrl.
93
+ const reader = useMemo<TaskGuaranteeReader | null>(() => {
94
+ if (injected) return injected;
95
+ if (!rpcUrl) return null;
96
+ return (pda) =>
97
+ fetchTaskGuarantee(
98
+ createSolanaRpc(rpcUrl) as unknown as ProgramAccountsSource,
99
+ pda as Address,
100
+ );
101
+ }, [injected, rpcUrl]);
102
+
103
+ const enabled =
104
+ (options?.enabled ?? true) && Boolean(taskPda) && reader !== null;
105
+
106
+ const query = useQuery<TaskGuarantee, Error>({
107
+ queryKey: queryKeys.taskGuarantee(
108
+ taskPda ? pdaKey(taskPda) : "",
109
+ ctx.cacheNamespace,
110
+ ),
111
+ enabled,
112
+ queryFn: () => reader!(taskPda as Address | string),
113
+ });
114
+
115
+ const guarantee = query.data ?? null;
116
+ return {
117
+ guarantee,
118
+ guaranteed: guarantee?.guaranteed ?? false,
119
+ isLoading: query.isLoading,
120
+ error: query.error ?? null,
121
+ refetch: () => void query.refetch(),
122
+ };
123
+ }
@@ -1,10 +1,14 @@
1
1
  import { useMutation } from "@tanstack/react-query";
2
- import { useCallback } from "react";
2
+ import { useCallback, useRef } from "react";
3
3
  import type { facade as facadeNs } from "@tetsuo-ai/marketplace-sdk";
4
4
  import { useAgencContext } from "../provider/context.js";
5
+ import type { MarketplaceClient } from "../types.js";
5
6
  import {
6
7
  mutationStatusOf,
7
8
  requireClient,
9
+ snapshotRecord,
10
+ snapshotRecordArray,
11
+ stabilizeSelectedTransactionSigner,
8
12
  type MutationStatus,
9
13
  } from "./internal.js";
10
14
 
@@ -28,6 +32,11 @@ export type AutoAcceptTaskResultInput = Omit<
28
32
  };
29
33
  export type TaskLifecycleStatus = MutationStatus;
30
34
 
35
+ interface QueuedTaskLifecycleInput<TInput> {
36
+ readonly client: MarketplaceClient;
37
+ readonly input: TInput;
38
+ }
39
+
31
40
  export interface UseTaskLifecycleResult {
32
41
  cancel: (input?: CancelTaskInput) => Promise<string>;
33
42
  close: (input?: CloseTaskInput) => Promise<string>;
@@ -43,55 +52,125 @@ export function useTaskLifecycle(
43
52
  taskPda: Parameters<typeof facadeNs.cancelTask>[0]["task"],
44
53
  ): UseTaskLifecycleResult {
45
54
  const ctx = useAgencContext();
46
- const cancelMut = useMutation<string, Error, CancelTaskInput | undefined>({
47
- mutationFn: async (input) => {
48
- const client = requireClient(ctx.client);
55
+ const lastAction = useRef<"cancel" | "close" | "autoAccept" | null>(null);
56
+ const cancelMut = useMutation<
57
+ string,
58
+ Error,
59
+ QueuedTaskLifecycleInput<CancelTaskInput>
60
+ >({
61
+ mutationFn: async ({ client, input }) => {
49
62
  const { signature } = await client.cancelTask({
50
- ...(input ?? {}),
63
+ ...input,
51
64
  task: taskPda,
52
- authority: input?.authority ?? client.signer,
53
65
  } as Parameters<typeof facadeNs.cancelTask>[0]);
54
66
  return signature;
55
67
  },
56
68
  });
57
- const closeMut = useMutation<string, Error, CloseTaskInput | undefined>({
58
- mutationFn: async (input) => {
59
- const client = requireClient(ctx.client);
69
+ const closeMut = useMutation<
70
+ string,
71
+ Error,
72
+ QueuedTaskLifecycleInput<CloseTaskInput>
73
+ >({
74
+ mutationFn: async ({ client, input }) => {
60
75
  const { signature } = await client.closeTask({
61
- ...(input ?? {}),
76
+ ...input,
62
77
  task: taskPda,
63
- authority: input?.authority ?? client.signer,
64
78
  } as Parameters<typeof facadeNs.closeTask>[0]);
65
79
  return signature;
66
80
  },
67
81
  });
68
- const autoAcceptMut = useMutation<string, Error, AutoAcceptTaskResultInput>({
69
- mutationFn: async (input) => {
70
- const client = requireClient(ctx.client);
82
+ const autoAcceptMut = useMutation<
83
+ string,
84
+ Error,
85
+ QueuedTaskLifecycleInput<AutoAcceptTaskResultInput>
86
+ >({
87
+ mutationFn: async ({ client, input }) => {
71
88
  const { signature } = await client.autoAcceptTaskResult({
72
89
  ...input,
73
90
  task: taskPda,
74
- authority: input.authority ?? client.signer,
75
91
  } as Parameters<typeof facadeNs.autoAcceptTaskResult>[0]);
76
92
  return signature;
77
93
  },
78
94
  });
79
95
 
80
96
  const cancel = useCallback(
81
- (input?: CancelTaskInput) => cancelMut.mutateAsync(input),
82
- [cancelMut],
97
+ async (input?: CancelTaskInput) => {
98
+ const client = requireClient(ctx.client);
99
+ const detachedInput = snapshotRecord(input ?? {}) as CancelTaskInput;
100
+ const authority = stabilizeSelectedTransactionSigner(
101
+ client.signer,
102
+ detachedInput.authority,
103
+ );
104
+ const snapshottedInput = snapshotRecord({
105
+ ...detachedInput,
106
+ authority,
107
+ // audit F5/F12: cancelTask requires the bond PDAs; the SDK facade derives
108
+ // them from authority / workerBondAuthority. Default the worker wallet to
109
+ // the task PDA itself — it can never be a bond poster (PDAs can't sign),
110
+ // so the derived worker-bond PDA is the empty no-op account.
111
+ workerBondAuthority: detachedInput.workerBondAuthority ?? taskPda,
112
+ ...(detachedInput.workerAccounts === undefined
113
+ ? {}
114
+ : {
115
+ workerAccounts: snapshotRecordArray(detachedInput.workerAccounts),
116
+ }),
117
+ ...(detachedInput.bidSettlement === undefined
118
+ ? {}
119
+ : { bidSettlement: snapshotRecord(detachedInput.bidSettlement) }),
120
+ }) as CancelTaskInput;
121
+ lastAction.current = "cancel";
122
+ return cancelMut.mutateAsync({ client, input: snapshottedInput });
123
+ },
124
+ [cancelMut, ctx.client],
83
125
  );
84
126
  const close = useCallback(
85
- (input?: CloseTaskInput) => closeMut.mutateAsync(input),
86
- [closeMut],
127
+ async (input?: CloseTaskInput) => {
128
+ const client = requireClient(ctx.client);
129
+ const detachedInput = snapshotRecord(input ?? {}) as CloseTaskInput;
130
+ const authority = stabilizeSelectedTransactionSigner(
131
+ client.signer,
132
+ detachedInput.authority,
133
+ );
134
+ const snapshottedInput = snapshotRecord({
135
+ ...detachedInput,
136
+ authority,
137
+ ...(detachedInput.children === undefined
138
+ ? {}
139
+ : { children: snapshotRecordArray(detachedInput.children) }),
140
+ }) as CloseTaskInput;
141
+ lastAction.current = "close";
142
+ return closeMut.mutateAsync({ client, input: snapshottedInput });
143
+ },
144
+ [closeMut, ctx.client],
87
145
  );
88
146
  const autoAccept = useCallback(
89
- (input: AutoAcceptTaskResultInput) => autoAcceptMut.mutateAsync(input),
90
- [autoAcceptMut],
147
+ async (input: AutoAcceptTaskResultInput) => {
148
+ const client = requireClient(ctx.client);
149
+ const detachedInput = snapshotRecord(input);
150
+ const authority = stabilizeSelectedTransactionSigner(
151
+ client.signer,
152
+ detachedInput.authority,
153
+ );
154
+ const snapshottedInput = snapshotRecord({
155
+ ...detachedInput,
156
+ authority,
157
+ ...(detachedInput.bidSettlement === undefined
158
+ ? {}
159
+ : { bidSettlement: snapshotRecord(detachedInput.bidSettlement) }),
160
+ }) as AutoAcceptTaskResultInput;
161
+ lastAction.current = "autoAccept";
162
+ return autoAcceptMut.mutateAsync({ client, input: snapshottedInput });
163
+ },
164
+ [autoAcceptMut, ctx.client],
91
165
  );
92
- const active = cancelMut.isPending || closeMut.isPending || autoAcceptMut.isPending;
166
+ const active =
167
+ cancelMut.isPending || closeMut.isPending || autoAcceptMut.isPending;
93
168
  const latest =
94
- [cancelMut, closeMut, autoAcceptMut].find((m) => m.isError || m.isSuccess) ?? cancelMut;
169
+ lastAction.current === "close"
170
+ ? closeMut
171
+ : lastAction.current === "autoAccept"
172
+ ? autoAcceptMut
173
+ : cancelMut;
95
174
 
96
175
  return {
97
176
  cancel,
@@ -99,9 +178,10 @@ export function useTaskLifecycle(
99
178
  autoAccept,
100
179
  status: active ? "pending" : mutationStatusOf(latest),
101
180
  signature: latest.data ?? null,
102
- error: cancelMut.error ?? closeMut.error ?? autoAcceptMut.error ?? null,
181
+ error: latest.error ?? null,
103
182
  isPending: active,
104
183
  reset: () => {
184
+ lastAction.current = null;
105
185
  cancelMut.reset();
106
186
  closeMut.reset();
107
187
  autoAcceptMut.reset();