File size: 5,676 Bytes
f109bf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

NB. =============================================================================
NB. Synthetic David — WorkItem / AgentResult sparse execution (PURE J)
NB. Locale: david. Selected agents only — no swarm fan-out.
NB. Product path: Prolog selected-set → dispatch_selected; EvidenceBundle only.
NB. Python ../execution stays smoke harness only.
NB. =============================================================================

cocurrent 'david'

NB. Evidence row (boxed fields):
NB.   0 evidence_id  1 source_id  2 source_type  3 content_hash  4 authority
NB.
NB. WorkItem (boxed fields):
NB.   0 request_id  1 work_id  2 agent_id  3 task
NB.   4 evidence_rows  5 required_outputs  6 permissions  7 deadline  8 risk_policy
NB.
NB. AgentResult (boxed fields):
NB.   0 agent_id  1 status  2 conclusions  3 evidence_refs  4 provenance_root

openscalar =: 3 : 0
  v =. y
  while. (32 = 3!:0 v) *. (0 = # $ v) do. v =. > v end.
  v
)

field =: 4 : 'openscalar x { y'

NB. Non-empty character / open content
nonempty =: 3 : 0
  v =. openscalar y
  if. 32 = 3!:0 v do. 0 return. end.
  0 < # v
)

NB. One evidence row is ok iff evidence_id, source_id, content_hash are non-empty.
NB. Bare text alone is never enough.
evidence_row_ok =: 3 : 0
  r =. y
  if. 3 > # r do. 0 return. end.
  (nonempty 0 { r) *. (nonempty 1 { r) *. (nonempty 3 { r)
)

NB. Bundle = list of boxed evidence rows; empty bundle is not ok for dispatch.
bundle_ok =: 3 : 0
  rows =. y
  if. 0 = # rows do. 0 return. end.
  i =. 0
  ok =. 1
  while. i < # rows do.
    ok =. ok *. evidence_row_ok openscalar i { rows
    i =. i + 1
  end.
  ok
)

uniqseq =: 3 : 0
  if. 0 = # y do. 0 $ a: return. end.
  ys =. boxopen y
  out =. 0 $ a:
  i =. 0
  while. i < # ys do.
    v =. openscalar i { ys
    if. -. (< v) e. out do. out =. out , < v end.
    i =. i + 1
  end.
  out
)

fields =: 4 : 0
  rows =. y
  i =. 0
  out =. 0 $ a:
  while. i < # rows do.
    row =. openscalar i { rows
    out =. out , < x field row
    i =. i + 1
  end.
  out
)

NB. Load selected agent ids from a text file (one id per line).
NB. Product handoff from Prolog / Spec J→swipl writer.
load_selected =: 3 : 0
  if. 0 = # (1!:0 < y) do. 0 $ a: return. end.
  raw =. 1!:1 < y
  lines =. <;._2 raw , LF
  out =. 0 $ a:
  i =. 0
  while. i < # lines do.
    s =. openscalar i { lines
    NB. drop CR if present (Windows handoff)
    if. (2 = 3!:0 s) *. (0 < # s) do.
      if. 13 = {: s do. s =. }: s end.
    end.
    if. (2 = 3!:0 s) *. (0 < # s) do. out =. out , < s end.
    i =. i + 1
  end.
  uniqseq out
)

NB. Evidence ids from validated bundle rows
bundle_ids =: 3 : 0
  rows =. y
  out =. 0 $ a:
  i =. 0
  while. i < # rows do.
    r =. openscalar i { rows
    out =. out , < 0 field r
    i =. i + 1
  end.
  out
)

NB. x = request: request_id ; task ; evidence_bundle ; required_outputs ; permissions ; risk_policy ; deadline
NB. y = selected agent_id list (from Prolog)
NB. Fails assert if bundle is not EvidenceBundle-ok (no bare text).
build_work_items =: 4 : 0
  bundle =. 2 field x
  NB. field peels the outer box; bundle is a list of evidence rows
  if. (32 = 3!:0 bundle) *. (0 = # $ bundle) do. bundle =. , < openscalar bundle end.
  assert. bundle_ok bundle
  aids =. uniqseq y
  rid =. 0 field x
  task =. 1 field x
  outs =. boxopen 3 field x
  perms =. boxopen 4 field x
  rpol =. 5 field x
  dl =. 6 field x
  wis =. 0 $ a:
  i =. 0
  while. i < # aids do.
    aid =. openscalar i { aids
    wid =. rid , ':' , aid
    wi =. (<rid) ; (<wid) ; (<aid) ; (<task) ; (<bundle) ; (<outs) ; (<perms) ; (<dl) ; (<rpol)
    wis =. wis , < wi
    i =. i + 1
  end.
  wis
)

make_stub =: 3 : 0
  aid =. 2 field y
  rid =. 0 field y
  brows =. boxopen 4 field y
  eids =. bundle_ids brows
  (<aid) ; (<'ok') ; (< 'stub:' , aid) ; (<eids) ; (< 'prov:' , rid , ':' , aid)
)

NB. x = request ; y = selected agent ids → AgentResult list (selected only)
dispatch_selected =: 4 : 0
  wis =. x build_work_items y
  rs =. 0 $ a:
  i =. 0
  while. i < # wis do.
    rs =. rs , < make_stub openscalar i { wis
    i =. i + 1
  end.
  rs
)

NB. Fixture EvidenceBundle rows (ids/hashes/authority — not bare text alone)
good_bundle =: 3 : 0
  e1 =. (<'ev_cobol_src_01') ; (<'src_cobol') ; (<'cobol_source') ; (<'hash_cobol') ; (<'0.9')
  e2 =. (<'ev_copybook_01') ; (<'src_copy') ; (<'copybook') ; (<'hash_copy') ; (<'0.85')
  e3 =. (<'ev_db2_schema_01') ; (<'src_db2') ; (<'db_schema') ; (<'hash_db2') ; (<'0.8')
  (<e1) , (<e2) , (<e3)
)

bad_bundle_no_hash =: 3 : 0
  e1 =. (<'ev_bare') ; (<'src_x') ; (<'text') ; (<'') ; (<'0.9')
  , < e1
)

smoke =: 3 : 0
  NB. Selected set from Prolog handoff file (written by run_execution_smoke.ijs)
  selected =. load_selected '../logs/selected_cobol.txt'
  assert. 0 < # selected
  assert. (< 'legacyCobol') e. selected
  assert. -. (< 'quant') e. selected
  assert. -. (< 'Quant') e. selected

  assert. bundle_ok good_bundle 0
  assert. -. bundle_ok bad_bundle_no_hash 0
  assert. -. bundle_ok (0 $ a:)

  req =. 'req-cobol-1' ; 'Explain COBOL balance vs Java' ; (< good_bundle 0) ; ('business_rules' ; 'equivalence_result') ; ('read_source' ; 'read_copybooks') ; 'escalate_on_high' ; 'none'
  wis =. req build_work_items selected
  wids =. 2 fields wis
  assert. selected -: wids
  assert. -. (< 'quant') e. wids
  rs =. req dispatch_selected selected
  rids =. 0 fields rs
  assert. selected -: rids
  assert. *./ (<'ok') = 1 fields rs
  assert. 0 = # (req dispatch_selected (0 $ a:))

  NB. Evidence refs on results are bundle ids, not bare text
  refs0 =. boxopen 3 field openscalar 0 { rs
  assert. (< 'ev_cobol_src_01') e. refs0

  'ok' ; (# selected) ; (# rs) ; (# refs0)
)